iOS 实现主题切换的最佳方案

作者: uniapp | 来源:发表于2019-01-15 20:02 被阅读8次

    投资证券的小伙伴儿,应该非常熟悉很多 App 的夜间模式。打开夜间模式时,App 所有的页面由白色变为黑色,这样做有两个好处:一个是可以保护眼睛;另一个是相关的红绿行情图像,也显得更加协调。Android 上称这种夜间/白天模式为「主题切换」, 实现方式大都是通过更改 Resource 文件夹的路径来实现。 作为一个iOS App 开发者, 如何实现这种主题切换的功能呢?

    GitHub 上面,star 最多的是叫 DKNightVersion 的开源库,内部原理是通过 Notification 的方式, 在 UIView 中监听通知变化,切换不同的主题颜色。也有一些其他的开源库,但是底层无一例外都使用了通知来实现。

    结合自己的项目,我想出了另外一种实现方式,原理如下:

    iOS 切换主题.png

    项目采用通常的 TabBarController 的展示方式。通过 SettingManager 管理 plist 文件,plist 中对应了不同主题的颜色。项目中所有 UIView 对象的颜色均通过 SettingManager 来设置。当切换不同主题时,通过 SettingManager 读取 plist 中对应的不同颜色值。

    上述方案的顺利实现,还需要以下 2 个细节:

    1 TabBarController 需要由 UINavigationController 包装一层,然后 UINavigationController 作为 window 的 rootViewController.

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        
        self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        self.window.backgroundColor = [UIColor whiteColor];
        
        UIStoryboard *storyBoard =  [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    
        ZDTabBarController *tabBarC = [storyBoard instantiateInitialViewController];
        tabBarC.view.backgroundColor = [UIColor themeColor];
        
        
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:tabBarC];
        nav.navigationBar.hidden = true;
        self.window.rootViewController = nav;
        [self.window makeKeyAndVisible];
        
        return YES;
    }
    

    2 Setting 页面需要以 pop 的方式弹出;与原来的 TabBarController 的 UINavigationController 相互独立;

    - (void)btnClick_start{
        MTSettingsViewController *settingsVC = [[MTSettingsViewController alloc] init];
        [settingsVC configuePushStyle:(PushStylePush)];
        [settingsVC show:self.navigationController];
    }
    

    有了以上两点儿的设计,在切换主题时,通过重新生成 TabBarController , 作为 UINavigationController 的 subViewControllers。

    + (void)configueTheme:(ThemeStyle) themeStyle rootViewController:(UIViewController *)vc completion:(void (^ __nullable)(void))completion{
        NSMutableDictionary *settingData = [[NSMutableDictionary alloc] initWithContentsOfFile:[self shareManager].plistPath];
        [settingData setValue:[NSNumber numberWithInteger:themeStyle] forKey:ThemeStyleKey];
        [settingData writeToFile:[self shareManager].plistPath atomically:true];
        
        UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
        [nav setViewControllers:@[vc]];
        
        if (completion) {
            completion();
        }
    }
    

    代码请移步 iOS 切换主题方案

    喜欢和关注都是对我的鼓励和支持~

    相关文章

      网友评论

        本文标题:iOS 实现主题切换的最佳方案

        本文链接:https://www.haomeiwen.com/subject/hqtudqtx.html