UITabBarController中自定义UITabBar
2015-09-02 08:01
429 查看
1.创建多个视图控制器,放如UITabBarController中
[objc] view
plaincopy
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
[objc] view
plaincopy
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的标题
[objc] view
plaincopy
aTabBarItem.title = @"aaa";
bTabBarItem.title = @"bbb";
cTabBarItem.title = @"ccc";
dTabBarItem.title = @"ddd";
5.设置tabBar中items的图片
[objc] view
plaincopy
[aTabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"aa_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"aa.png"]];
[bTabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"bb_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"bb.png"]];
[cTabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"cc_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"cc.png"]];
[dTabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"dd_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"dd.png"]];
6.设置tabBar的背景图片
[objc] view
plaincopy
// Change the tab bar background
UIImage* tabBarBackground = [UIImage imageNamed:@"tabbarbg.png"];
[[UITabBar appearance] setBackgroundImage:[tabBarBackground resizableImageWithCapInsets:UIEdgeInsetsZero]];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"tabbar_selected.png"]];
7.改变tabBar中items上字体的颜色
[objc] view
plaincopy
// Change the title color of tab bar 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中
[objc] view
plaincopy
self.window.rootViewController = tabBarController;
[self.window makeKeyAndVisible];
[objc] view
plaincopy
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
[objc] view
plaincopy
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的标题
[objc] view
plaincopy
aTabBarItem.title = @"aaa";
bTabBarItem.title = @"bbb";
cTabBarItem.title = @"ccc";
dTabBarItem.title = @"ddd";
5.设置tabBar中items的图片
[objc] view
plaincopy
[aTabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"aa_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"aa.png"]];
[bTabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"bb_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"bb.png"]];
[cTabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"cc_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"cc.png"]];
[dTabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"dd_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"dd.png"]];
6.设置tabBar的背景图片
[objc] view
plaincopy
// Change the tab bar background
UIImage* tabBarBackground = [UIImage imageNamed:@"tabbarbg.png"];
[[UITabBar appearance] setBackgroundImage:[tabBarBackground resizableImageWithCapInsets:UIEdgeInsetsZero]];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"tabbar_selected.png"]];
7.改变tabBar中items上字体的颜色
[objc] view
plaincopy
// Change the title color of tab bar 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中
[objc] view
plaincopy
self.window.rootViewController = tabBarController;
[self.window makeKeyAndVisible];
相关文章推荐
- [Leetcode]#60 Permutation Sequence
- LeetCode_DP_Unique Binary Search Trees II
- key对应VALUE
- usaco Sorting a Three-Valued Sequence
- hdu5412CRB and Queries
- UITESTLIST
- UIView,UIButton,UIImageView等视图设置圆角,设置阴影,设置边框的方法
- HDOJ 题目3397 Sequence operation(线段树区间覆盖异或合并)
- ios开发之UI基础--应用管理xib-九宫格布局
- C#StringBuilder的使用
- LeetCode_DP_Unique Binary Search Trees
- POJ2299 Ultra-QuickSort(树状数组)
- iOS 7 新特性 - UIKit Dynamics
- Prefer Literal Syntax over the Equivalent methods
- UItableCell 设计
- ios开发之UI基础--懒加载
- POJ Ultra-QuickSort (树状数组+离散化)
- UIImageview的contentmode介绍
- LeetCode_DP_Unique Paths II
- Leetcode Unique Binary Search Trees