2-配置tabbar
先搭建骨架,底部tabbar,切換控制器
先建立TabBarController
UIViewController *vc01 = [[UIViewController alloc] init];
vc01.tabBarItem.title = @"精華";
vc01.tabBarItem.image = [UIImage imageNamed:@"tabBar_essence_icon"];
UIImage * image = [UIImage imageNamed:@"tabBar_essence_click_icon"];
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
vc01.tabBarItem.selectedImage = image;
vc01.view.backgroundColor = [UIColor redColor];
[self addChildViewController:vc01];
UIViewController *vc02 = [[UIViewController alloc] init];
vc02.tabBarItem.title = @"新帖";
vc02.tabBarItem.image = [UIImage imageNamed:@"tabBar_new_icon"];
vc02.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_new_click_icon"];
vc02.view.backgroundColor = [UIColor orangeColor];
[self addChildViewController:vc02];
UIViewController *vc03 = [[UIViewController alloc] init];
vc03.tabBarItem.title = @"關注";
vc03.tabBarItem.image = [UIImage imageNamed:@"tabBar_friendTrends_icon"];
vc03.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_friendTrends_click_icon"];
vc03.view.backgroundColor = [UIColor yellowColor];
[self addChildViewController:vc03];
UIViewController *vc04 = [[UIViewController alloc] init];
vc04.tabBarItem.title = @"我";
vc04.tabBarItem.image = [UIImage imageNamed:@"tabBar_me_icon"];
vc04.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_me_click_icon"];
vc04.view.backgroundColor = [UIColor greenColor];
[self addChildViewController:vc04];
但是,有沒有覺得有大量重複程式碼,顯得很爛啊,稍等再精簡一下下吧
由於蘋果給tabbar渲染過效果,所以tabbar看起來是
與要求不符,要做修改
方式一
UIViewController *vc01 = [[UIViewController alloc] init];
vc01.tabBarItem.title = @"精華";
vc01.tabBarItem.image = [UIImage imageNamed:@"tabBar_essence_icon"];
UIImage * image = [UIImage imageNamed:@"tabBar_essence_click_icon"];
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//RenderingMode渲染模式 原始
vc01.tabBarItem.selectedImage = image;
vc01.view.backgroundColor = [UIColor redColor];
[self addChildViewController:vc01];
效果圖
但是這種程式碼還是這麼多,還得寫四個,不想寫啊。還有,如果以後這個圖片用到其他地方也不想被渲染,就還得寫一遍
好的,那麼在找一個簡單一點的方法吧,
如圖,找到需要的圖片選中
就好了
緊接著還有一個問題,( ⊙o⊙ )?文字怎麼還是藍色的?!既然設定文字和檢視屬性用tabbarItem,那麼文字顏色呢
UIViewController *vc01 = [[UIViewController alloc] init];
vc01.tabBarItem.title = @"精華";
vc01.tabBarItem.image = [UIImage imageNamed:@"tabBar_essence_icon"];
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
attrs[NSForegroundColorAttributeName] = [UIColor grayColor];
[vc01.tabBarItem setTitleTextAttributes:attrs forState:UIControlStateNormal];
NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
selectedAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
selectedAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
[vc01.tabBarItem setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
vc01.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_essence_click_icon"];
vc01.view.backgroundColor = [UIColor redColor];
[self addChildViewController:vc01];
文字顏色就可以改變了,但是還得寫4個
[vc02.tabBarItem setTitleTextAttributes:attrs forState:UIControlStateNormal];
[vc02.tabBarItem setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
[vc03.tabBarItem setTitleTextAttributes:attrs forState:UIControlStateNormal];
[vc03.tabBarItem setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
[vc04.tabBarItem setTitleTextAttributes:attrs forState:UIControlStateNormal];
[vc04.tabBarItem setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
所以要優化一下(一改全改)
進入方法中檢視,其結尾有
UI_APPEARANCE_SELECTOR
凡是帶有UI_APPEARANCE_SELECTOR巨集的,都可以用另一種方法去設定,就是拿到APPEARANCE。那麼先把上邊01,02,03,04設定文字顏色的方法註釋掉,
//通過appearance統一設定所有UITabBarItem的文字屬性
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
attrs[NSForegroundColorAttributeName] = [UIColor grayColor];
NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
selectedAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
selectedAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
//好處就是,只要給appearance設定,以後都是一個效果
UITabBarItem * item = [UITabBarItem appearance];//appearance外觀
[item setTitleTextAttributes:attrs forState:(UIControlStateNormal)];
[item setTitleTextAttributes:selectedAttrs forState:(UIControlStateSelected)];
//必須結尾有UI_APPEARANCE_SELECTOR
相關文章
- TabBartabBar
- 直播小程式原始碼,配置tabbar底部導航欄原始碼tabBar
- tabBar DisplaytabBar
- 替換tabBartabBar
- 自定義 tabBartabBar
- 小程式tabBar跳轉頁面並隱藏tabBartabBar
- Flutter 自定義 TabBarFluttertabBar
- iOS自定義tabBariOStabBar
- 凸出按鈕的TabBartabBar
- vue 自定義元件tabbarVue元件tabBar
- iOS 隱藏&顯示tabBariOStabBar
- 小程式tabBar帶文字展示tabBar
- 微信小程式自定義tabBar微信小程式tabBar
- 微信小程式 自定義tabbar微信小程式tabBar
- 微信小程式--》tabBar底部欄微信小程式tabBar
- 集合框架2- ArrayList框架
- 實戰2-注入
- 機器學習2-特徵工程機器學習特徵工程
- tabbar凸起點選事件處理tabBar事件
- Flutter 仿掘金之動態TabbarFluttertabBar
- Flutter使用TabBar問題小結FluttertabBar
- Flutter深入淺出元件篇---TabBarFlutter元件tabBar
- 從支付寶tabbar看BEMtabBar
- 彩色 TabBar 切換動畫實現tabBar動畫
- 2-服務發現
- MVC + EFCore 專案實戰 - 數倉管理系統2- 搭建基本框架配置EFCoreMVC框架
- 實現QQ的TabBar拖拽動效tabBar
- 小程式tabbar這套方案全搞定!tabBar
- qml 導航欄TabBar 工具欄ToolBartabBar
- 自定義react-navigation的TabBarReactNavigationtabBar
- 微信應用號tabBar不顯示tabBar
- 微信小程式tabBar顯示問題微信小程式tabBar
- 計算機視覺2-> 深度學習 | anaconda+cuda+pytorch環境配置計算機視覺深度學習PyTorch
- iOS OC-自定義TabBar TabBarViewControlleriOStabBarViewController
- iOS 關於tabBar的幾處筆記iOStabBar筆記
- 18. vue-router案例-tabBar導航VuetabBar
- 小程式底部導航tabBar不顯示tabBar
- 2-色彩空間轉換