首页 > 代码库 > iOS_17_控制器切换_TabBarController_通过storyboard方式

iOS_17_控制器切换_TabBarController_通过storyboard方式

最终效果图:



main.storyboard



BeyondViewController.m中有一句关键代码,设置tabbarItem图片的样式(30*30)

//
//  BeyondViewController.m
//  17_控制器切换2_tabbarController
//
//  Created by beyond on 14-7-31.
//  Copyright (c) 2014年 com.beyond. All rights reserved.
//

#import "BeyondViewController.h"
#import "NanaViewController.h"
#import "SettingViewController.h"
@interface BeyondViewController ()

@end

@implementation BeyondViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    NSLog(@"view did loaded");
    
    // UITabbarController里面的tabbarItem中的图片需要特殊API处理之后,才能正常显示
    UIImage *img = [UIImage imageNamed:@"home"];
    UIImage *img_selected = [UIImage imageNamed:@"home_s"];
    // 设置图片 渲染 模式
    img = [img imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    // 设置图片 渲染 模式
    img_selected = [img_selected imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    // 构造方法生成 UITabBarItem
    UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"" image:img selectedImage:img_selected];
    
    // 设置当前控制器的 tabBarItem属性
    self.tabBarItem = item;
    self.tabBarItem.title = @"首页";
    self.tabBarItem.badgeValue = http://www.mamicode.com/[NSString stringWithFormat:@"%d",1] ;>

NanaViewController.h

//
//  NanaViewController.h
//  17_控制器切换2_tabbarController
//
//  Created by beyond on 14-7-31.
//  Copyright (c) 2014年 com.beyond. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface NanaViewController : UIViewController


// 自定义方法,设置自己的tabbaritem样式
- (void) setTabBarItemDIY;
@end


NanaViewController.m



//
//  NanaViewController.m
//  17_控制器切换2_tabbarController
//
//  Created by beyond on 14-7-31.
//  Copyright (c) 2014年 com.beyond. All rights reserved.
//

#import "NanaViewController.h"

@interface NanaViewController ()

@end

@implementation NanaViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSLog(@"view did loaded  2");
    
    
}
// 自定义方法,设置自己的tabbaritem样式
- (void) setTabBarItemDIY
{
    // UITabbarController里面的tabbarItem中的图片需要特殊API处理之后,才能正常显示
    UIImage *img = [UIImage imageNamed:@"nana"];
    UIImage *img_selected = [UIImage imageNamed:@"nana_s"];
    // 设置图片 渲染 模式
    img = [img imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    // 设置图片 渲染 模式
    img_selected = [img_selected imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    // 构造方法生成 UITabBarItem
    UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"" image:img selectedImage:img_selected];
    
    // 设置当前控制器的 tabBarItem属性
    self.tabBarItem = item;
    self.tabBarItem.title = @"娜娜";
    self.tabBarItem.badgeValue = http://www.mamicode.com/[NSString stringWithFormat:@"%d",2] ;>

SettingViewController.h

//
//  SettingViewController.h
//  17_控制器切换2_tabbarController
//
//  Created by beyond on 14-7-31.
//  Copyright (c) 2014年 com.beyond. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface SettingViewController : UIViewController

// 自定义方法,设置自己的tabbaritem样式
- (void) setTabBarItemDIY;
@end


SettingViewController.m

//
//  SettingViewController.m
//  17_控制器切换2_tabbarController
//
//  Created by beyond on 14-7-31.
//  Copyright (c) 2014年 com.beyond. All rights reserved.
//

#import "SettingViewController.h"

@interface SettingViewController ()

@end

@implementation SettingViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSLog(@"view did loaded  3");
    
    
}

// 自定义方法,设置自己的tabbaritem样式
- (void) setTabBarItemDIY
{
    // UITabbarController里面的tabbarItem中的图片需要特殊API处理之后,才能正常显示
    UIImage *img = [UIImage imageNamed:@"setting"];
    UIImage *img_selected = [UIImage imageNamed:@"setting_s"];
    // 设置图片 渲染 模式
    img = [img imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    // 设置图片 渲染 模式
    img_selected = [img_selected imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    // 构造方法生成 UITabBarItem
    UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"" image:img selectedImage:img_selected];
    
    // 设置当前控制器的 tabBarItem属性
    self.tabBarItem = item;
    self.tabBarItem.title = @"我的";
    self.tabBarItem.badgeValue = http://www.mamicode.com/[NSString stringWithFormat:@"%d",3] ;>