0

When I set the background color of a UITabBar iOS automatically lightens this color since the default UITabBar is translucent.

But I would like to use a UITabBar which is NOT translucent. In iOS 12 and below I solved this by setting a background image of the desired color:

// Create an image from a given color using a custom extension
[[UITabBar appearance] setBackgroundImage:[UIImage colorImageWithColor:[UIColor redColor]]];

This works fine. However, I would like to use the new Dark Mode in iOS 13. Obviously this cannot be done when using a colored background image instead of a background color. Instead not without reacting manually to the appearance change to switch to another color image.

Using named colors would be way better IF it would be possible to tell iOS not to draw the `UITabBar translucent.


If I try to disable the translucent effect the UITabBar become all white instead of the specified color.

[[UITabBar appearance] setTranslucent:false];

How to solve this?

Andrei Herford
  • 17,570
  • 19
  • 91
  • 225

1 Answers1

0

I managed to solve the problem using a custom UITabBar subclass.

  • When the app launches the dynamic color MyDynamicTabBarBG is set as non-translucent background by creating an image from this color
  • Changes between Normal and Dark Mode while the app is active are detected using traitCollectionDidChange. Than the dynamic color is simply re-applied by creating a new image.

Code:

@implementation MCTabBar

- (void)awakeFromNib {
    [super awakeFromNib];

    // Create a color image from a given color using a custom extension
    [self setBackgroundImage:[UIImage colorImageWithColor:[UIColor colorNamed:@"MyDynamicTabBarBG"]]];
}

- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
    [self setBackgroundImage:[UIImage colorImageWithColor:[UIColor colorNamed:@"MyDynamicTabBarBG"]]];
}

@end

This works but is quite hacky. Is there really no more elegant solution?

Andrei Herford
  • 17,570
  • 19
  • 91
  • 225