我正在我的应用程序中实现管理iOS 13 暗模式的功能。我的 ViewController 的背景有问题。
我的视图控制器具有使用CAGradientLayer
.
我设法根据用户的选择更改构成渐变的颜色,当它从暗模式 ---> 亮模式和亮模式 ---> 暗模式..
我的问题是,当用户在后台发送我的应用程序以转到控制中心并更改模式时,我用于背景颜色的渐变颜色不会立即更改...
要获得渐变颜色更改,用户必须关闭应用程序并重新打开它。
一个非常糟糕的用户体验,所以我想问你如何解决这个问题......
这是我用来根据用户选择的iOS
模式更改渐变颜色的方法
- (void)viewDidLoad {
[super viewDidLoad];
[self setupBackground];
}
- (void)setupBackground {
UIColor *secondaryColor = self.view.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark ? UIColor.customRedColor : UIColor.customGreenColor;
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = UIApplication.sharedApplication.windows.firstObject.bounds;
gradient.colors = @[(id)UIColor.customBlueColor.CGColor, (id)secondaryColor.CGColor];
gradient.locations = @[@0.1, @0.9];
[self.view.layer insertSublayer:gradient atIndex:0];
}