UITabBar changes background color of one UITabBarItem on iOS7

Can I change the background color of a specific UITabBarItem to a UITabBar ?

I know how to change the entire background of a selected background using:

 [[UITabBar appearance] setBarTintColor:[UIColor redColor]]; [[UITabBar appearance] setTintColor:[UIColor blueColor]]; [[UITabBar appearance] setSelectedImageTintColor:[UIColor yellowColor]]; 

But can this be done only for one element without subclasses?

thanks

+7
source share
5 answers

You can add a subview to the parent tabBar and set the background color in the preview. You can use the tabBar frame dimensions to calculate the offset and width of your tabBarItem, and then insert a preview below it.

Example (in Swift):

 // Add background color to middle tabBarItem let itemIndex = 2 let bgColor = UIColor(red: 0.08, green: 0.726, blue: 0.702, alpha: 1.0) let itemWidth = tabBar.frame.width / CGFloat(tabBar.items!.count) let bgView = UIView(frame: CGRectMake(itemWidth * itemIndex, 0, itemWidth, tabBar.frame.height)) bgView.backgroundColor = bgColor tabBar.insertSubview(bgView, atIndex: 0) 
+29
source

in Swift: This is a solution for applications using Auto Layout . The main difference between the other solutions is that tabBar.frame.width does not get the actual width of the device screen. Thus, bgView appears in the wrong place.

You can fix this with this: UIScreen.main.bounds.width

 let tabWidth: CGFloat = UIScreen.main.bounds.width / CGFloat(self.tabbar!.items!.count) let tabIndex: CGFloat = 2 let bgColor: UIColor = .redColor let bgView = UIView(frame: CGRectMake(tabWidth * tabIndex, 0, tabWidth, 49)) bgView.backgroundColor = bgColor self.tabbar!.insertSubview(bgView, atIndex: 0) 

49 - UITabbar height by UITabbar

+6
source

Objective-C Solution:

 int itemIndex = 3; UIColor* bgColor = [UIColor colorWithRed:(245/255.f) green:(192/255.f) blue:(47/255.f) alpha:1]; float itemWidth = self.tabBarController.tabBar.frame.size.width / 5.0f; //5 = tab bar items UIView* bgView = [[UIView alloc]initWithFrame: CGRectMake(itemWidth*itemIndex, 0,itemWidth, self.tabBarController.tabBar.frame.size.height)]; bgView.backgroundColor = bgColor; [self.tabBarController.tabBar insertSubview:bgView atIndex:1]; 
+3
source

You cannot do this with the hue color property. See this post that even setSelectedImageTintColor really doesn't work (this is not the last time I checked).

The solution is to change tintColor on the fly for the item in question. You can do this ad hoc whenever the selected item is changed by implementing the UITabBarDelegate tabBar:didSelectItem: method, for example. in the applicationโ€™s application.

0
source

Updated for Swift 4:

 let itemIndex: CGFloat = 2.0 let bgColor = UIColor.ownBlue let itemWidth = tabBar.frame.width / CGFloat(tabBar.items!.count) let bgView = UIView(frame: CGRect(x: itemWidth * itemIndex, y: 0, width: itemWidth, height: tabBar.frame.height)) bgView.backgroundColor = bgColor tabBar.insertSubview(bgView, at: 0) 
0
source

Source: https://habr.com/ru/post/959420/


All Articles