- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在努力将 iOS 13 深色模式支持添加到我的 iOS 11+ 应用程序中。在整个应用程序中使用命名/动态颜色效果很好。
但是,在自定义类中使用 [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
我是否明确指定traitCollection
/UIUserInterfaceStyle
并不重要。即使 UIUserInterfaceStyleDark
处于事件状态,也仅返回浅色。
我错过了什么吗?
还有其他方法可以明确指定我想要访问的颜色值吗?
最佳答案
您从 Assets 目录中获取的 UIColor
是动态。这意味着其 RGB 分量的值取决于 [UITraitCollection currentTraitCollection]
的值。每次您请求组件时,颜色都会根据 currentTraitCollection
自行解析。
您没有显示 -colorAsHexString:
方法的实现,但它一定以某种方式从颜色中获取 RGB 分量。
因此,您需要在设置 currentTraitCollection
时调用 -colorAsHexString:
,如下所示:
UIColor *dynamicColor = [UIColor colorNamed:@"whiteOrBlack"];
[self.traitCollection performAsCurrentTraitCollection:^{
NSLog(@"Color: %@", [self colorAsHexString:dynamicColor]);
}];
(更好的是,您可以将对 performAsCurrentTraitCollection
的调用放在 -colorAsHexString:
的实现中,如果这在您的具体情况下有意义的话。)
以下是如何为特定特征集合获取解析的非动态颜色:
UIColor *dynamicColor = [UIColor colorNamed:@"whiteOrBlack"];
UIColor *resolvedColor = [dynamicColor resolvedColorWithTraitCollection:self.traitCollection];
关于ios - 只有从 [UIColor colorNamed :] even though traitCollection. userInterfaceStyle = Dark 返回的浅色外观颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58733875/
我正在我的 playground Android 应用中实现一个深色主题,并且正在努力将操作栏文本颜色设置为白色。 以下是我的风格和颜色。操作栏的背景沿用了colorPrimary,很棒。然而,两种颜
我正在尝试创建一个 HTML 页面,单击按钮时,主题会从深色变为浅色。 我遇到的问题是菜单图标。如果您更改为深色主题,然后打开和关闭菜单,则浅色和深色主题菜单图标显示或弹出菜单不会在其中显示任何内容,
我正在尝试找到正确的颜色。例如,如果我有深绿色,那么我需要浅绿色,或者如果我有浅绿色,那么我需要深绿色。我有一个代码可以告诉我颜色是深色还是浅色。 function calcBrightness(re
比如我发现即使不加粗体的Raleway字体,我也可以设置fontWeight: FontWeight.bold并且可以正常工作。那么为什么我们要添加粗体和黑色版本的字体呢? 最佳答案 当您将 font
我在来自 here 的游戏中使用 CCBlade这会在滑动时产生 Blade 效果,在黑屏上效果很好。但是当我尝试在较浅的彩色屏幕上使用它时遇到了问题,我的游戏有淡蓝色的天空颜色。我认为这与 CCBl
我怎样才能以某种方式显示在 Firebug 中显示的不可见标签的样式?我不知道为什么它们是苍白/看不见的,我需要找出原因。 最佳答案 Firebug 淡出那些在浏览器中不可见或隐藏的元素。根据此处显示
所以在 Angular Material 2 中,我设置了主题 $store-primary: mat-palette($mat-storecast); $store-accent: mat-pal
在 macOS 10.14 Mojave 中,创建自动绘制明暗版本的 NSImage 实例的唯一方法是通过 Assets 目录和 +[NSImage imageNamed:] .但是,我需要在运行时创
我在 createMuiTheme 中设置了调色板类型:深色,并将背景颜色更改为深色,这很好。但文本颜色保持黑色。难道不应该采用更浅的颜色吗? CodeSandbox 链接:https://codes
我的应用支持 iOS 13 深色模式,并为用户提供了匹配系统外观或强制应用始终使用深色模式或浅色模式的选项,无论系统设置如何。 该应用还允许在用户按下 UILabel 时显示上下文菜单。但是,当使用
如何在应用程序中切换主题?如果我覆盖默认样式怎么办?我也可以分别为明暗主题定义不同的样式吗?为什么图标 colos 会改变 - 即如果主题设置为深色,图标的黑色背景会变成白色。如果我将应用程序栏的背景
Link: https://50.62.213.246/gktest/ 搜索栏中的占位符文本和输入文本不会响应font-weight: 300;如果我尝试 font-weight: 700;文字是粗体
我是一名优秀的程序员,十分优秀!