最后效果图:

iOS_17_控制开关_TabBarController_由storyboard道路-LMLPHP

main.storyboard

iOS_17_控制开关_TabBarController_由storyboard道路-LMLPHP

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 = [NSString stringWithFormat:@"%d",1] ; // 默认情况下,app执行之后,仅仅执行第一个控制器的view did load方法,因此,要得到UITabBarController容器中全部的控制器,调用其自己定义的一个方法,设置它们自己的tabbaritem样式 // 先得到父容器UITabBarController,有了它,就有了全部的控制器实例的引用
UITabBarController *parentCtrl = self.parentViewController;
// 得到容器UITabBarController中全部的子控制器
NSArray *children = [parentCtrl childViewControllers];
// 调用相应的子控制器的 自己定义方法,设置它们自己的tabbaritem样式
NanaViewController *nanaVC = (NanaViewController *)[children objectAtIndex:1];
[nanaVC setTabBarItemDIY]; // 调用相应的子控制器的 自己定义方法,设置它们自己的tabbaritem样式
SettingViewController *setVC = (SettingViewController *)[children objectAtIndex:2];
[setVC setTabBarItemDIY]; } @end

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 = [NSString stringWithFormat:@"%d",2] ;
} @end

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 = [NSString stringWithFormat:@"%d",3] ;
}
@end

版权声明:本文博客原创文章。博客,未经同意,不得转载。

05-11 21:45