目前主流app架構(隻是大多數)
一般tabar設定的内容
常見的建立方式
方式一:
- 此處隻是列出預設圖檔,如果需要選中圖檔,可以在方法中自己再添加。
- 優點:可以拿到對應的子控制器,進行單獨定制操作、
- (void)addAllChildViewController {
UIViewController *homeVC = [[UIViewController alloc] init];
homeVC.view.backgroundColor = [UIColor redColor];
[self addChildViewController:homeVC title:@"首頁" imageNamed:@"tabBar_home"];
UIViewController *activityVC = [[UIViewController alloc] init];
activityVC.view.backgroundColor = [UIColor yellowColor];
[self addChildViewController:activityVC title:@"活動" imageNamed:@"tabBar_activity"];
UIViewController *findVC = [[UIViewController alloc] init];
findVC.view.backgroundColor = [UIColor blueColor];
[self addChildViewController:findVC title:@"發現" imageNamed:@"tabBar_find"];
UIViewController *mineVC = [[UIViewController alloc] init];
mineVC.view.backgroundColor = [UIColor greenColor];
[self addChildViewController:mineVC title:@"我的" imageNamed:@"tabBar_mine"];
}
// 添加某個 childViewController
- (void)addChildViewController:(UIViewController *)vc title:(NSString *)title imageNamed:(NSString *)imageNamed {
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
nav.tabBarItem.title = title;
nav.tabBarItem.image = [UIImage imageNamed:imageNamed];
[self addChildViewController:nav];
}
方式二:
- 對
屬性數組進行設定,通過一個NSObject,對一些屬性進行指派、viewControllers
- 缺點:不能對單個的子控制器進行操作
- 自定義tabbar 需要單獨重新布局 Demo位址
導航标題的設定影響UItabBar的标題
在開發中,經常遇到tabbar标題和導航标題一樣了,怎麼回事?
當self.navigationItem.title,self.tabBarItem.title沒有指派情況下,其值和
self.title
一緻(這句話還得區分場景)。
- self.title 在
init
中設定
代碼會先走init,此時再設定self.tabBarItem.title,就可以保證導航标題和底部tabbar标題不一緻。
或者直接設定self.navigationItem.title也可以到達此效果
- self.title 是否在
viewDidload
中設定
會先走self.tabBarItem.title,後調用viewDidload,這樣就會再次覆寫掉底部的标題
有一定經驗的程式猿,一定會使用系統自己的tabbar,這樣即使遇到了系統更新,一些坑點的時候,會很好處理。
比如:自定義特殊tabbar在iPhoneX上的适配問題
使用系統tabbar.png
自定義tabbar.png
當然有些項目肯定有些需要自定義,而且一些特殊的app是有很酷炫的動畫效果,這裡羅列一個demo
Demo
參考文章:
https://www.jianshu.com/p/a3002314db32
https://www.jianshu.com/p/914f3a568ec9
https://my.oschina.net/u/2340880/blog/
https://blog.csdn.net/Xia0Liang/article/details/50373603
附:
-
//試圖數組
NSArray* controllerArr = @[@"HomeViewController",@"MessageViewController",@"LifeViewController",@"MineViewController"];
//标題數組
NSArray* titleArr = @[@"首頁",@"資訊披露",@"生活",@"我的"];
//圖檔數組
NSArray* picArr = @[@"home",@"info",@"life",@"my"];
NSArray* xzpicArr = @[@"home-a",@"info-a",@"life-a",@"my-a"];
NSMutableArray* array = [[NSMutableArray alloc]init];
for(int i=0; i<picArr.count; i++)
{
Class cl=NSClassFromString(controllerArr[i]);
UIViewController* controller = [[cl alloc]init];
UINavigationController* nv = [[UINavigationController alloc]initWithRootViewController:controller];
controller.title = titleArr[i];
nv.tabBarItem.image = [[UIImage imageNamed:[NSString stringWithFormat:@"%@",picArr[i]]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//設定選中時的圖檔
nv.tabBarItem.selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"%@",xzpicArr[i]]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//設定選中時字型的顔色(也可更改字型大小)
[nv.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]} forState:UIControlStateSelected];
[array addObject:nv];
}
self.viewControllers = array;