Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITabBar / UITabBarItem and iOS 13's Dark Mode

In our app, we use custom UITabBarItem dynamically (see below) programmatically. We use ImageAssets to get the dark/light right images. But it doesn't work at 100% : if we restart the app, it's fine. If iOS switch to dark mode and the app is running, images keep the light mode. Wondering if it's a bug or if I can manage to make it work now...

UIImage *mImage = [UIImage imageNamed:@"tabBar1"];
UIImage *mImageSel = [UIImage imageNamed:@"tabBar1Sel"];

mImage = [mImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
mImageSel = [mImageSel imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

self.tabBarItem = [[UITabBarItem alloc] initWithTitle:nil image:mImage selectedImage:mImageSel];

UIEdgeInsets titleInsets = UIEdgeInsetsMake(-6, 0.0, -6.0, 0.0);
self.tabBarItem.imageInsets = titleInsets;

No error messages are displayed...

like image 749
Didier Avatar asked Jul 12 '19 17:07

Didier


1 Answers

We're seeing the same issue with programmatically created UITabBarItems.

We tried recreating the TabBarItem within UIViewController.traitCollectionDidChange(:) and the issue persists. Seems like an Apple bug. Notably, the issue only occurs for SelectedImage for us. The default, non-selected state seems to respect UIUserInterfaceStyle changes (dark mode).

We found a temporary solution: if you reassign the same selectedImage to myViewController.tabBarItem within UIViewController.traitCollectionDidChange(:) the issue resolves.

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
  super.traitCollectionDidChange(previousTraitCollection)
  self.tabBarItem.selectedImage = mySelectedImage //same image asset that was used in the initializer
}

Not sure why this would fix it but it works for us.

like image 196
Bleu Haus Nick Avatar answered Oct 04 '22 00:10

Bleu Haus Nick