iOS dark mode adaptation

iOS dark mode adaptation

Not fit dark mode

Added info directly in a field

User Interface Style

Setting value

Light

Dark adaptation mode

Adaptation black mode field inside info that do not add up

But the need for each UIWindow,UIViewController,UIViewincrease following this method inside

-(void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection

In this method, go to judge and change the color of all elements of the current interface

I specialize in a single case, all color applications are saved separately

There are two methods, namely, all the corresponding color to light or dark

In fact, a Color, light is a color value, is another dark color values, an application can define multiple Color

All applications are taken from the color of the singleton

By the time adjustment to the singleton just need to click on the line interface refresh

-(void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection
{
    [super traitCollectionDidChange: previousTraitCollection];
    if (@available(iOS 13.0, *))
    {
        if(UITraitCollection.currentTraitCollection.userInterfaceStyle == UIUserInterfaceStyleDark)
        {
            [[Color darkColor] setDarkColorArray];
            LRLog(@"暗黑模式");
        }
        else
        {
            [[Color darkColor] setBrightColorArray];
            LRLog(@"正常模式");
        }
        UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
        [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[Color darkColor].dataColor2,NSFontAttributeName:[UIFont systemFontOfSize:18]}];
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[Color darkColor].backColor] forBarMetrics:(UIBarMetricsDefault)];
        [self.navigationController.navigationBar setShadowImage:[UIImage imageWithColor:[Color darkColor].backColor]];
        [self.navigationController.navigationBar setBarTintColor:[Color darkColor].backColor];
        window.backgroundColor=[Color darkColor].backColor;
        self.view.backgroundColor=[Color darkColor].backColor;
    }
}

Demo Institute Add https://github.com/YouZhiZheShiJingCheng/dark

Guess you like

Origin blog.51cto.com/2254359459/2481580