简体   繁体   English

[UIColor colorNamed:] 仅返回浅色外观颜色,即使 traitCollection.userInterfaceStyle = Dark

[英]Only light appearance color returned from [UIColor colorNamed:] even though traitCollection.userInterfaceStyle = Dark

I am working on adding iOS 13 Dark Mode support to my iOS 11+ app.我正在努力将 iOS 13 暗模式支持添加到我的 iOS 11+ 应用程序中。 The works great using named / dynamic colors all over the app.在整个应用程序中使用命名/动态 colors 效果很好。

However, when using [UIColor colorNamed:] in a custom class always the light color version ( #ffffff / white) is returned instead of the dark version ( #000000 / black):但是,在自定义 class 中使用 [UIColor colorNamed:] 时,始终返回浅色版本( #ffffff / 白色)而不是深色版本( #000000 / 黑色):

// Some ViewController
CustomClass *custom = [customClass alloc] initWithTraitCollection:self.traitCollection]; 

- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
    [super traitCollectionDidChange:previousTraitCollection];
    custom.traitCollection = self.traitCollection;
}


// CustomClass
- (void)initWithTraitCollection:(UITraitCollection *)traitCollection {
    self = [super init];
    if (self) {
        self.traitCollection = traitCollection;
    }
    return self;
}

- (void)doSomething {    
    NSLog(@"CustomClass UIStyle: %@", (self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark ? @"dark" : (self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleLight ? @"light" : @"unspecified")));

    // Trying to get the color in three different ways (normal + specify traitCollection explicitly)
    UIColor *color1 = [UIColor colorNamed:@"whiteOrBlack"];
    UIColor *color2 = [UIColor colorNamed:@"whiteOrBlack" inBundle:nil compatibleWithTraitCollection:self.traitCollection];

    __block UIColor *color3 = color1;
    [self.traitCollection performAsCurrentTraitCollection:^{
         color3 = [UIColor colorNamed:@"whiteOrBlack"];
    }];

    // Output
    NSLog(@"   color1: %@", [self colorAsHexString:color1]);
    NSLog(@"   color2: %@", [self colorAsHexString:color2]);
    NSLog(@"   color3: %@", [self colorAsHexString:color3]);
}


// Output
CustomClass UIStyle: dark
   #ffffff
   #ffffff
   #ffffff

It does not matter if I specify the traitCollection / UIUserInterfaceStyle explictly or not.我是否明确指定traitCollection / UIUserInterfaceStyle并不重要。 Only the light color is returned, even if UIUserInterfaceStyleDark is active.即使UIUserInterfaceStyleDark处于活动状态,也只会返回浅色。

Am I missing something?我错过了什么吗?

Is there any other way so explicitly specify which color value I would like to access?有没有其他方法可以如此明确地指定我想要访问的颜色值?

The UIColor that you are fetching from the asset catalog is dynamic .您从资产目录中获取的UIColor动态的。 That means that the values of its RGB components depend on the value of [UITraitCollection currentTraitCollection] .这意味着其 RGB 组件的值取决于[UITraitCollection currentTraitCollection]的值。 Each time you ask for the components, the color resolves itself based on the currentTraitCollection .每次您请求组件时,颜色都会根据currentTraitCollection自行解析。

You didn't show the implementation of your -colorAsHexString: method, but it must be getting the RGB components from the color, somehow.您没有展示您的-colorAsHexString:方法的实现,但它必须以某种方式从颜色中获取 RGB 分量。

Therefore, you want to call -colorAsHexString: at a time when you have set the currentTraitCollection , like this:因此,您想在设置currentTraitCollection时调用-colorAsHexString: ,如下所示:

    UIColor *dynamicColor = [UIColor colorNamed:@"whiteOrBlack"];

    [self.traitCollection performAsCurrentTraitCollection:^{
         NSLog(@"Color: %@", [self colorAsHexString:dynamicColor]);
    }];

(Even better, you could put the call to performAsCurrentTraitCollection inside the implementation of -colorAsHexString: , if that makes sense in your specific case.) (更好的是,您可以将对performAsCurrentTraitCollection的调用放入-colorAsHexString:的实现中,如果这在您的特定情况下有意义的话。)

Here's how to get a resolved, non-dynamic color for a specific trait collection:以下是如何为特定特征集合获取已解析的非动态颜色:

    UIColor *dynamicColor = [UIColor colorNamed:@"whiteOrBlack"];
    UIColor *resolvedColor = [dynamicColor resolvedColorWithTraitCollection:self.traitCollection];

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

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