通过全局变量和监听来实现夜间模式

通过在AppDelegate.h文件中设置一个属性

@property (nonatomic, assign) BOOL isNightMode;

然后在切换模式的地方改变它的值。

if (isNight) 
{
    //正在日间模式
    cell.textLabel.text = @"夜间模式";
    ((AppDelegate *)[UIApplication sharedApplication].delegate).inNightMode = NO;
} else {
    //正在夜间模式
    cell.textLabel.text = @"日间模式";
    ((AppDelegate *)[UIApplication sharedApplication].delegate).inNightMode = YES;
}
[[NSNotificationCenter defaultCenter] postNotificationName:@"dawnAndNight" object:nil];
isNight = !isNight;

通过实现其方法来得到相应的效果

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dawnAndNightMode:) name:@"dawnAndNight" object:nil];

颜色可通过判断来选择

+ (UIColor *)themeColor
{
    if (((AppDelegate *)[UIApplication sharedApplication].delegate).inNightMode) {
        return [UIColor colorWithRed:0.17 green:0.17 blue:0.17 alpha:1.0];
    }
    return [UIColor colorWithRed:235.0/255 green:235.0/255 blue:243.0/255 alpha:1.0];
}

直接调用themeColor即可

你可能感兴趣的:(通过全局变量和监听来实现夜间模式)