当select项目时,如何更改UITabItem的背景颜色

当用户select一个标签栏项目时,我想要一个不同的背景颜色,而不是当它被选中。

把它放在application didFinishLaunchingWithOptionsAppdelegate.m

 UIImage *whiteBackground = [UIImage imageNamed:@"whiteBackground"]; [[UITabBar appearance] setSelectionIndicatorImage:whiteBackground]; 

如果您使用故事板或xibs,请单击“选项卡栏”并将“selectedImageTintColor”path添加到“关键path属性”标记中。 喜欢这个 :

在这里输入图像说明

更新:从iOS 7.1起,此技术不再有效(如果用户连续两次轻击相同的选项卡,背景色将被清除)。


UITabBarItemUIBarItem一个子类,一切都比较痛苦,因为UIBarItem不能inheritanceUIView ; 但是, UITabBarItem 包含一个。 以下操纵该视图,因此如果提交给AppStore可能会被拒绝。

1)子类UITabBarItem

创build一个UITabBarItem的子类并添加一个新的selected属性到它的头部,如下所示:

 @interface ALDTabBarItem : UITabBarItem @property (nonatomic, assign, getter = isSelected) BOOL selected; @end 

UITabBarItems有一个视图属性,但没有公开。 我们可以扩展这个类来访问它,然后在selected属性上创build一个自定义的setter来更改背景颜色,如下所示:

 #import "ALDTabBarItem.h" @interface ALDTabBarItem (ALD) @property (nonatomic, strong) UIView *view; @end @implementation ALDTabBarItem - (void)setSelected:(BOOL)selected { if(selected) self.view.backgroundColor = [UIColor redColor]; else self.view.backgroundColor = [UIColor clearColor]; } @end 

2)更新你的UITabBarController委托

将下面的代码添加到您的UITabBarController的委托,它设置UITabBar的选定状态:

 - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item { for(ALDTabBarItem *myItem in tabBar.items) myItem.selected = (myItem == item); } 

在Swift中

 UITabBar.appearance().selectionIndicatorImage = UIImage(named: "tabSelected") 

与一个大小98×98像素的图像tabSelected@2x.png

遵循以下步骤:

  1. 创buildUITabBarController子类

  2. 转到viewDidAppearUITabBarController子类

  3. 现在findTabBarItem的大小,

     UITabBar *tabBar = self.tabBar; CGSize imgSize = CGSizeMake(tabBar.frame.size.width/tabBar.items.count,tabBar.frame.size.height); 
  4. 现在创build这个大小的图像,

     //Create Image UIGraphicsBeginImageContextWithOptions(imgSize, NO, 0); UIBezierPath* p = [UIBezierPath bezierPathWithRect:CGRectMake(0,0,imgSize.width,imgSize.height)]; [[UIColor blueColor] setFill]; [p fill]; UIImage* finalImg = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); 
  5. 现在,将此图像分配给TabBar的SelectionIndicatorImage

     [tabBar setSelectionIndicatorImage:finalImg]; 

请参阅下面的url。

改变UITabBar的Tint / Background颜色

如何更改Xcode中的标签栏颜色

希望对你有帮助..

尝试这个来改变tabbar项目的颜色,但它只能在ios5中工作。

 if ([UITabBar instancesRespondToSelector:@selector(setSelectedImageTintColor:)]) { [tabBarController.tabBar setSelectedImageTintColor:[UIColor redColor]]; } 

我的答案类似于@Mehul Thakkar,但它是在Swift 4中,并改善他的答案我会说,如果你把代码放在viewDidAppear ,他build议用户会看到发生的变化,这是不好的用户体验。

因此,为您的TabBar控制器创build自定义类,并在viewDidLoad放置以下代码:

 let singleTabWidth: CGFloat = self.tabBar.frame.size.width / CGFloat((self.tabBar.items?.count)!) let singleTabSize = CGSize(width:singleTabWidth , height: self.tabBar.frame.size.height) let selectedTabBackgroundImage: UIImage = self.imageWithColor(color: .white, size: singleTabSize) self.tabBar.selectionIndicatorImage = selectedTabBackgroundImage 

imageWithColor函数在你下面:

 //image with color and size func imageWithColor(color: UIColor, size: CGSize) -> UIImage { let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height) UIGraphicsBeginImageContext(rect.size) let context = UIGraphicsGetCurrentContext() context!.setFillColor(color.cgColor) context!.fill(rect) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image! } 

希望这有助于某人。

你可以使用tintcolor。

 [[UITabBar appearance] setSelectedImageTintColor:[UIColor redColor]]; 

在AppDelegate.m中,在应用程序启动后,//覆盖自定义点之后放置以下代码。

把它放在你的AppDelegate.m文件中:

  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. [UITabBar appearance].selectionIndicatorImage = [UIImage imageNamed:@"activeTabBackgroundImage"]; return YES; } 

目前在Xcode 8.3.2中,您可以使用代表实际背景的图像在故事板中执行此操作。

select标签栏控制器中的标签栏:

在这里输入图像说明

在实用程序中select属性检查器并更改select背景图像:

在这里输入图像说明