iOS中 UITabBarController中自定义UITabBar
2015-11-14 08:39
603 查看
1.创建多个视图控制器,放如UITabBarController中
AViewController *aa = [[AViewController alloc] init];
UINavigationController* ayNav = [[UINavigationController alloc]initWithRootViewController:aa];
BViewController *bb = [[BViewController alloc] init];
UINavigationController* bNav = [[UINavigationController alloc]initWithRootViewController:bb];
CViewController *cc = [[CViewController alloc] init];
UINavigationController* cNav = [[UINavigationController alloc]initWithRootViewController:cc];
DViewController *dd = [[DViewController alloc] init];
UINavigationController* dNav = [[UINavigationController alloc]initWithRootViewController:dd];
2.初始化tabbar
UITabBarController *tabBarController = [[UITabBarController alloc]init];
tabBarController.delegate=self;
tabBarController.viewControllers=[[NSArray alloc]initWithObjects:ayNav,bNav,cNav,dNav,nil];
3.获取到tabBarController中的tabBar,在从tabBar中获取到每个items
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *aTabBarItem = [tabBar.items objectAtIndex:0];
UITabBarItem *bTabBarItem = [tabBar.items objectAtIndex:1];
UITabBarItem *cTabBarItem = [tabBar.items objectAtIndex:2];
UITabBarItem *dTabBarItem = [tabBar.items objectAtIndex:3];
4. 设置tabBar中items的标题
aTabBarItem.title = @"小韩哥";
bTabBarItem.title = @"iOS编程";
cTabBarItem.title = @"ios开发";
dTabBarItem.title = @"苹果系统";
5.设置tabBar中items的图片
[aTabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"aa_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"1.png"]];
[bTabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"bb_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"2.png"]];
[cTabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"cc_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"3.png"]];
[dTabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"dd_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"4.png"]];
6.设置tabBar的背景图片
UIImage* tabBarBackground = [UIImage imageNamed:@"tabbar1.png"];
[[UITabBar appearance] setBackgroundImage:[tabBarBackground resizableImageWithCapInsets:UIEdgeInsetsZero]];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"tabbar.png"]];
7.改变tabBar中items上字体的颜色
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor grayColor], UITextAttributeTextColor,
nil nil] forState:UIControlStateNormal];
UIColor *titleHighlightedColor = [UIColor colorWithRed:153/255.0 green:192/255.0 blue:48/255.0 alpha:1.0];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
titleHighlightedColor, UITextAttributeTextColor,
nil nil] forState:UIControlStateHighlighted];
8.将tabBarController加入window中
self.window.rootViewController = tabBarController;
[self.window makeKeyAndVisible];
AViewController *aa = [[AViewController alloc] init];
UINavigationController* ayNav = [[UINavigationController alloc]initWithRootViewController:aa];
BViewController *bb = [[BViewController alloc] init];
UINavigationController* bNav = [[UINavigationController alloc]initWithRootViewController:bb];
CViewController *cc = [[CViewController alloc] init];
UINavigationController* cNav = [[UINavigationController alloc]initWithRootViewController:cc];
DViewController *dd = [[DViewController alloc] init];
UINavigationController* dNav = [[UINavigationController alloc]initWithRootViewController:dd];
2.初始化tabbar
UITabBarController *tabBarController = [[UITabBarController alloc]init];
tabBarController.delegate=self;
tabBarController.viewControllers=[[NSArray alloc]initWithObjects:ayNav,bNav,cNav,dNav,nil];
3.获取到tabBarController中的tabBar,在从tabBar中获取到每个items
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *aTabBarItem = [tabBar.items objectAtIndex:0];
UITabBarItem *bTabBarItem = [tabBar.items objectAtIndex:1];
UITabBarItem *cTabBarItem = [tabBar.items objectAtIndex:2];
UITabBarItem *dTabBarItem = [tabBar.items objectAtIndex:3];
4. 设置tabBar中items的标题
aTabBarItem.title = @"小韩哥";
bTabBarItem.title = @"iOS编程";
cTabBarItem.title = @"ios开发";
dTabBarItem.title = @"苹果系统";
5.设置tabBar中items的图片
[aTabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"aa_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"1.png"]];
[bTabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"bb_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"2.png"]];
[cTabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"cc_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"3.png"]];
[dTabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"dd_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"4.png"]];
6.设置tabBar的背景图片
UIImage* tabBarBackground = [UIImage imageNamed:@"tabbar1.png"];
[[UITabBar appearance] setBackgroundImage:[tabBarBackground resizableImageWithCapInsets:UIEdgeInsetsZero]];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"tabbar.png"]];
7.改变tabBar中items上字体的颜色
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor grayColor], UITextAttributeTextColor,
nil nil] forState:UIControlStateNormal];
UIColor *titleHighlightedColor = [UIColor colorWithRed:153/255.0 green:192/255.0 blue:48/255.0 alpha:1.0];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
titleHighlightedColor, UITextAttributeTextColor,
nil nil] forState:UIControlStateHighlighted];
8.将tabBarController加入window中
self.window.rootViewController = tabBarController;
[self.window makeKeyAndVisible];
相关文章推荐
- iOS中 UITabBarController中自定义UITabBar
- Educational Codeforces Round 1 B. Queries on a String
- Definition Questions
- Leetcode Range Sum Query 2D - Immutable
- Leetcode Range Sum Query - Immutable
- 10005---servlet request请求对象常用方法总结
- GPUImage API文档之GLProgram类
- iOS之 基于UIImageView的图片浏览器
- poj2299 Ultra-QuickSort
- POJ - 1986 Distance Queries(离线Tarjan算法)
- 关于UEdiutor文本编辑器。前台展示分文字的问题
- OS X下UNIX环境高级编程(第三版)学习日志-第一章ChapterI,编译apue包与第一个例程
- 禁止uiscrollview垂直方向滚动,只允许水平方向滚动;或只允许垂直方向滚动
- UE4蓝图案例:制作按下键盘F键改变场景中模型的颜色
- iOS 8 Auto Layout界面自动布局系列2-使用Xcode的Interface Builder添加布局约束
- 在T-SQL语句中访问远程数据库(openrowset/opendatasource/openquery)
- IOS的UI基础01
- hdoj 2817 A sequence of numbers【快速幂】
- Codeforces 597C Subsequences 【树状数组优化DP】
- HDU 5493 Queue(线段树)