關於iOS7-8的系統UITabBarController,uitabbarcontroller
眼看iOS9就要出了。最近做項目用的系統的UITabBarController,就mark一下遇到的問題。
系統預設的UITabBarController使用storyboard可以進行設定。
下面主要是講代碼設定tabbar的圖片與顏色。
1.tabbar的背景顏色或背景圖(灰色效果)
tabBarController.tabBar.backgroundImage = [UIImage imageNamed:@"tabbar_unselected"];
這個圖片可以是一張寬度為2像素,高98的一個細線圖。
2.設定選中item的背景顏色或背景圖(,藍色效果)
UIImage* selectedImage = [UIImage imageNamed:@"tabbar_select"]; selectedImage = [selectedImage resizableImageWithCapInsets:UIEdgeInsetsZero]; tabBarController.tabBar.selectionIndicatorImage = selectedImage;
這裡的圖片也可以跟上面的背景圖片一樣,可以是一張寬度為2像素,高98的一個細線圖。
selectedImage = [selectedImage resizableImageWithCapInsets:UIEdgeInsetsZero];
是為了用細線圖填充要設定的image
3.設定圖片顏色與字型顏色及字型大小
NSMutableDictionary *textAttrs=[NSMutableDictionary dictionary]; textAttrs[NSForegroundColorAttributeName]=[UIColor whiteColor]; textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:12.f];
MLNavigationController * oneController = [[MLNavigationController alloc] initWithRootViewController:VC1];
oneController.tabBarItem.title = @"首頁";
oneController.tabBarItem.image = [[UIImage imageNamed:@"tabbar_home"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
oneController.tabBarItem.selectedImage = [[UIImage imageNamed:@"tabbar_home"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[oneController.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
我的需求是設定圖片與顏色在選中與未選中狀態下都為白色。
3.1字型顏色設定
字典textAttrs的key值可以雙擊去查看你要設定的選項來設定,這裡注意一點
- (void)setTitleTextAttributes:(NSDictionary *)attributes forState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
這個方法state只能設定UIControlStateNormal,UIControlStateSelected,UIControlStateDisabled三個type
3.2圖片顏色設定
圖片你會發現用了一個image的方法
- (UIImage *)imageWithRenderingMode:(UIImageRenderingMode)renderingMode NS_AVAILABLE_IOS(7_0);
這個方法是告訴系統取消渲染,使用原本的圖片顏色。
這樣你就可以用你提供的selectedImage與image進行設定了。
oneController.tabBarItem.image = [[UIImage imageNamed:@"tabbar_home"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
oneController.tabBarItem.selectedImage = [[UIImage imageNamed:@"tabbar_home"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];