标签:
@interface UITabBarController : UIViewController <UITabBarDelegate, NSCoding>
在application: idFinishLaunchingWithOptions:方法中创建
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible];
1 UITabBarController *mainTabBar = [[UITabBarController alloc] init]; 2 3 // 创建控制器对象 4 UIViewController *firstVC = [[UIViewController alloc] init]; 5 6 firstVC.view.backgroundColor = [UIColor cyanColor]; 7 8 // 设置tabBarItem 9 // 第一种方式:系统样式 10 firstVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:100]; 11 12 // 第二种方式:自定义样式 13 UIViewController *secondVC = [[UIViewController alloc] init]; 14 15 secondVC.view.backgroundColor = [UIColor redColor]; 16 17 // 创建图片 18 UIImage *secondImage = [UIImage imageNamed:@"carGary"]; 19 20 UIImage *secondSelectImage = [UIImage imageNamed:@"carRed"]; 21 22 #pragma mark - 设置图片保留原有样式 23 secondImage = [secondImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 24 secondSelectImage = [secondSelectImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 25 26 #pragma mark - 27 secondVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"第二页" image:secondImage selectedImage:secondSelectImage]; 28 29 // thirdVC 30 UIViewController *thirdVC = [[UIViewController alloc] init]; 31 32 thirdVC.view.backgroundColor = [UIColor purpleColor]; 33 34 thirdVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"发现" image:[UIImage imageNamed:@"findGray"] tag:101]; 35 36 // fourthVC 37 UIViewController *fourthVC = [[UIViewController alloc] init]; 38 39 fourthVC.view.backgroundColor = [UIColor greenColor]; 40 41 fourthVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:[UIImage imageNamed:@"userGray"] tag:102]; 42 43 // fifthVC 44 UIViewController *fifthVC = [[UIViewController alloc] init]; 45 46 fifthVC.view.backgroundColor = [UIColor orangeColor]; 47 48 fifthVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:103]; 49 50 // sixthVC 51 UIViewController *sixthVC = [[UIViewController alloc] init]; 52 53 sixthVC.view.backgroundColor = [UIColor magentaColor]; 54 55 sixthVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemHistory tag:104]; 56 57 // 设置控制器数组 58 mainTabBar.viewControllers = @[firstVC, secondVC, thirdVC];
1 self.window.rootViewController = mainTabBar;
viewControllers属性的应用件 3> ② 的代码
// 设置进入应用时选中第几个 mainTabBar.selectedIndex = 1;
UITabBar 包含多个 UITabBarItem , 每个 UITabBarItem 对应一个 UIViewController
UITabBar 的高度是 49
系统最多只显示 5 个 UITabBarItem , 当 UITabBarItem 超过 5 个时系统会自动增加一个更多按钮, 点击更多按钮, 没有在底部出现的按钮会以 列表 的形式显示出来
UITabBar的属性: tintColor , barTintColor , 图像设置等
1 // tabBar的属性 2 3 // 设置选中的颜色 4 mainTabBar.tabBar.tintColor = [UIColor greenColor]; 5 6 // 是否打开半透明效果 7 mainTabBar.tabBar.translucent = NO; 8 9 // 设置tabBar的颜色 10 // mainTabBar.tabBar.barTintColor = [UIColor grayColor];
UITabBarItem 可以通过属性 title , badgeValue 设置标题及提示
// 设置提示 thirdVC.tabBarItem.badgeValue = @"有消息";
UITabBarItem 的创建
① 系统样式
// 第一种方式:系统样式 firstVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:100];
② 自定义样式
// 第二种方式:自定义样式 secondVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"第二页" image:secondImage selectedImage:secondSelectImage];
secondImage 和 secondSelectImage 是两个 UIImage 类型的变量
1 // 创建图片 2 UIImage *secondImage = [UIImage imageNamed:@"carGary"]; 3 4 UIImage *secondSelectImage = [UIImage imageNamed:@"carRed"]; 5 6 #pragma mark - 设置图片保留原有样式 7 secondImage = [secondImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 8 secondSelectImage = [secondSelectImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
1> 概述
如果想通过一键设定所有导航试图控制器的颜色, 类似于QQ的一键换肤操作,可以通过UIAppearance 协议 来进行操作, 通过它可以对一些控件进行定义颜色等。
2> 使用代码
1 // 设置全局外观 2 // 通过[UITabBar appearance]得到当前应用的UITabBar对象来设置tabBar的外观 3 // 注意:设置全局外观最好在appDelegate ,否则会无效 4 [[UITabBar appearance] setBarTintColor:[UIColor cyanColor]]; [[UITabBar appearance] setTintColor:[UIColor brownColor]]; 5 // 改变导航栏外观颜 6 [[UINavigationBar appearance] setBarTintColor:[UIColor lightGrayColor]]; 7 // 改变导航栏字体颜 8 [[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor],NSForegroundColorAttributeName, [UIFont systemFontOfSize:17], NSFontAttributeName, nil]];
标签:
原文地址:http://www.cnblogs.com/gfxxbk/p/5403260.html