1、颜色
self.backgroundColor = [UIColor systemRedColor];
UIColor *color = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) {
if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
// 深色模式下的颜色
return [UIColor blueColor];
}
else {
// 浅色模式下的颜色
return [UIColor purpleColor];
}
}];
self.backgroundColor = color;
3、模式变化监听
- 在UIViewController、UIView上重写以下方法进行监听。
// 重写该方法监听模式的改变
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
[super traitCollectionDidChange:previousTraitCollection];
NSLog(@"traitCollectionDidChange");
// 创建动态 color
UIColor *color = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) {
if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
return [UIColor darkGrayColor];
}
else {
return [UIColor redColor];
}
}];
self.backgroundColor = color;
}