Set Background Color of Active Tab Bar Item in Swift

Set background color of active tab bar item in Swift

I took a similar approach to @matcartmill but without the need for a special image. This solution is just based on your color.

// set red as selected background color
let numberOfItems = CGFloat(tabBar.items!.count)
let tabBarItemSize = CGSize(width: tabBar.frame.width / numberOfItems, height: tabBar.frame.height)
tabBar.selectionIndicatorImage = UIImage.imageWithColor(color: UIColor.red, size: tabBarItemSize).resizableImage(withCapInsets: UIEdgeInsets.zero)

// remove default border
tabBar.frame.size.width = self.view.frame.width + 4
tabBar.frame.origin.x = -2

I'm making use of the following extension of UIImage:

extension UIImage {

class func imageWithColor(color: UIColor, size: CGSize) -> UIImage {
let rect: CGRect = CGRectMake(0, 0, size.width, size.height)
UIGraphicsBeginImageContextWithOptions(size, false, 0)
color.setFill()
UIRectFill(rect)
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}

}

I hope this helps!

for swift 4

extension UIImage {

class func imageWithColor(color: UIColor, size: CGSize) -> UIImage {
let rect: CGRect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
UIGraphicsBeginImageContextWithOptions(size, false, 0)
color.setFill()
UIRectFill(rect)
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return image
}
}

UITabBarItem Tab Bar Item Individual Selected Background Color

how can I change the selected background color for individual tab bar items

Tab bar items do not have a "selected background color". What they have, which you can set, is a selectedImage. By setting this to an image whose rendering mode is .AlwaysOriginal, you can dictate the look of the entire tab bar item image when it is selected, as opposed to when it is not selected.

How to change the background color of tab bar programatically?

You should use self.tabBar.barTintColor or have a look at UIBarStyle and self.tabBar.barStyle and see if that works.

Edit background color of specific Tab bar item

You can use the following code :

// 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)

For reference you can look into these Stackoverflow posts :

  • Change background Color of One UITabBarItem
  • Set background Color of Active Tab bar item

Setting the background color of a specific tab bar Item and making it a seperate item

@interface BaseViewController : UITabBarController

// Create a view controller and setup it's tab bar item with a title and image
-(UIViewController*) viewControllerWithTabTitle:(NSString*)title image:(UIImage*)image;

// Create a custom UIButton and add it to the center of our tab bar
-(void) addCenterButtonWithImage:(UIImage*)buttonImage highlightImage: (UIImage*)highlightImage;

@implementation BaseViewController

-(UIViewController*) viewControllerWithTabTitle:(NSString*) title image:(UIImage*)image
{
UIViewController* viewController = [[UIViewController alloc] init] ;
viewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:image tag:0];

return viewController;
}

// Create a custom UIButton and add it to the center of our tab bar

  -(void) addCenterButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage
{
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button setBackgroundImage:highlightImage forState:UIControlStateHighlighted];

CGFloat heightDifference = buttonImage.size.height - self.tabBar.frame.size.height;
if (heightDifference < 0)
button.center = self.tabBar.center;
else
{
center = self.tabBar.center;
center.y = center.y - heightDifference/2.0;
button.center = center;
}

[self.view addSubview:button];
}

Subclass this Tab controller and call the above function to set image o tab bar items and center button like Instagram

  @interface InstagramViewController : BaseViewController

@end

@implementation InstagramViewController

- (void)viewDidLoad
{
[super viewDidLoad];

self.viewControllers = [NSArray arrayWithObjects:
[self viewControllerWithTabTitle:@"Feed" image:[UIImage imageNamed:@"112-group.png"]],
[self viewControllerWithTabTitle:@"Popular" image:[UIImage imageNamed:@"29-heart.png"]],
[self viewControllerWithTabTitle:@"Share" image:nil],
[self viewControllerWithTabTitle:@"News" image:[UIImage imageNamed:@"news.png"]],
[self viewControllerWithTabTitle:@"@user" image:[UIImage imageNamed:@"123-id-card.png"]], nil];
}

-(void)willAppearIn:(UINavigationController *)navigationController
{

[self addCenterButtonWithImage:[UIImage imageNamed:@"cameraTabBarItem.png"] highlightImage:nil];
}

@end



Related Topics



Leave a reply



Submit