专题名:UITabBarController
UI:UITabBarItem图片尺寸:30*30px。
一、创建TabBae:
1、根页面创建:
UITabBarController如果要作为整个程序的rootViewController,则在application delegate中的 applicationDidFinishLaunching:方法中添加如下代码:(以下代码带导航栏)
HSHomeViewController *hsHome = [[HSHomeViewController alloc]init];
UINavigationController *homeNav = [[UINavigationController alloc]initWithRootViewController:hsHome];
HSOpenInfoViewController *hsOpen = [[HSOpenInfoViewController alloc]init];
UINavigationController *openNav = [[UINavigationController alloc]initWithRootViewController:hsOpen];
HSMyInfoViewController *hsMy = [[HSMyInfoViewController alloc]init];
UINavigationController *myNav = [[UINavigationController alloc]initWithRootViewController:hsMy];
HSMoreViewController *hsMore = [[HSMoreViewController alloc]init];
UINavigationController *moreNav = [[UINavigationController alloc]initWithRootViewController:hsMore];
UITabBarController * tabBar = [[UITabBarController alloc]init];
tabBar.viewControllers =[NSArray arrayWithObjects:homeNav,openNav,myNav,moreNav, nil];
self.window.rootViewController = tabBar;
二、改变每个tab的内容:
1、IOS7.0以上版本:创建方法:
UITabBarItem *item1 = [[UITabBarItem alloc]initWithTitle:@"彩票大厅" image:[UIImage imageNamed:@"tab_home_false.png"] selectedImage:[UIImage imageNamed:@"tab_home_true.png"]];
<pre name="code" class="objc">viewController2.tabBarItem = item;
IOS7.0以下版本:创建方法:
UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"Second" image:nil tag:2];
[item setFinishedSelectedImage:[UIImage imageNamed:@"second.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"first.png"]];
viewController2.tabBarItem = item;
2、属性badgeValue,该属性可以在其右上角显示一个小的角标,通常用于提示用户有新的消息。
3、改变tabBarItem的文字颜色:
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor grayColor], UITextAttributeTextColor, nil] forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor], UITextAttributeTextColor, nil] forState:UIControlStateHighlighted];
4、改变tabBar的图标颜色:
UITabBar *tabBar = [tabBarCtr tabBar];
[tabBar setTintColor:[UIColor redColor]];