Using UIAppearance and switching themes

孤街浪徒 提交于 2019-11-30 18:02:29

问题


I'm looking to theme my iOS app and have been reading up on UIAppearance. I want the user to be able to switch between a number of different visual themes from within the app. Changing a theme would then be shown in the UI.

I'm thinking I could have a theme file that is a singleton loaded within the appDelegate.m. But after that i'm a little stuck on how this could be implemented?


回答1:


UIKit sets properties from UIAppearance proxy after view is added to views hierarchy.

In UISS I use method like this:

- (void)reloadAppearance {
    NSArray * windows = [UIApplication sharedApplication].windows;

    for (UIWindow *window in windows) {
        for (UIView *view in window.subviews) {
            [view removeFromSuperview];
            [window addSubview:view];
        }
    }
}

Another trick is to remove rootViewController from main window and add it again. Though I prefer the first solution, because it covers wider range of cases.




回答2:


This works for me in Swift:

let windows = UIApplication.sharedApplication().windows
for window in windows {
    for view in window.subviews {
        view.removeFromSuperview()
        window.addSubview(view)
    }
}



回答3:


The solution suggested by Robert, doesn't work for me . I'm using iOS9 and UITabbarController . This suggestion is the only that worked from answer here

It basically explained the problem well, when we use UIAppearance, it basically is applied and will not change until the views is redrawn again which will not happen especially with rootViewController like UITabbarControllers and new colors will only have effect ONLY AFTER the App is removed from memory and opened again, if you want an immediate change , you will have to make it by hand and setting each property like described in the answer in attached link




回答4:


swift 4:

let windows = UIApplication.shared.windows
for window in windows {
    for view in window.subviews {
        view.removeFromSuperview()
        window.addSubview(view)
    }
}


来源:https://stackoverflow.com/questions/17070582/using-uiappearance-and-switching-themes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!