本文講述tabBar的自定制,設置tabBar的items的圖文,代碼如下:
- (void)addChildViewController:(UIViewController *)viewController imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName title:(NSString *)title{
viewController.tabBarItem.image = [UIImage imageNamed:imageName];//normal - image
viewController.tabBarItem.title = title;//設置title
//改變UITabBarItem 字體顏色
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor lightGrayColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];//normal - titleColor
[[UITabBarItem appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:247/255.0 green:120/255.0 blue:122/255.0 alpha:1.0],NSForegroundColorAttributeName, nil]forState:UIControlStateSelected];//selected - titleColor
UIImage *image = [UIImage imageNamed:selectedImageName];
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];//顯示原圖配置,去渲染影響
viewController.tabBarItem.selectedImage = image;//selected - image
// viewController.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);//移動tabBar圖片
viewController.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, -5);//移動tabBar文字
[self addChildViewController:viewController];
}
更多分享,敬請關注!
荊軻刺秦王!