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...
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With