简体   繁体   English

使用 iOS 13 暗模式时如何使 UITabBar 不透明?

[英]How to make UITabBar NOT translucent while using iOS 13 Dark Mode?

When I set the background color of a UITabBar iOS automatically lightens this color since the default UITabBar is translucent.当我设置UITabBar的背景颜色时,iOS 会自动使这种颜色变亮,因为默认的UITabBar是半透明的。

But I would like to use a UITabBar which is NOT translucent .但我想使用一个不透明UITabBar In iOS 12 and below I solved this by setting a background image of the desired color:在 iOS 12 及以下我通过设置所需颜色的背景图像解决了这个问题:

// 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.但是,我想在 iOS 13 中使用新的暗模式。显然,当使用彩色背景图像而不是背景颜色时,这是无法做到的。 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.如果可以告诉 iOS 不要绘制 `UITabBar 半透明,则使用命名为 colors 会更好。


If I try to disable the translucent effect the UITabBar become all white instead of the specified color.如果我尝试禁用半透明效果, UITabBar将变为全白而不是指定颜色。

[[UITabBar appearance] setTranslucent:false];

How to solve this?如何解决这个问题?

I managed to solve the problem using a custom UITabBar subclass.我设法使用自定义UITabBar子类解决了这个问题。

  • When the app launches the dynamic color MyDynamicTabBarBG is set as non-translucent background by creating an image from this color当应用程序启动动态颜色时, MyDynamicTabBarBG通过从该颜色创建图像设置为非半透明背景
  • Changes between Normal and Dark Mode while the app is active are detected using traitCollectionDidChange .使用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.这可行,但很hacky。 Is there really no more elegant solution?真的没有更优雅的解决方案了吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM