Using UIAppearance and switching themes

2020-03-03 10:04发布

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?

4条回答
疯言疯语
2楼-- · 2020-03-03 10:04

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

查看更多
我欲成王,谁敢阻挡
3楼-- · 2020-03-03 10:13

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)
    }
}
查看更多
爱情/是我丢掉的垃圾
4楼-- · 2020-03-03 10:21

swift 4:

let windows = UIApplication.shared.windows
for window in windows {
    for view in window.subviews {
        view.removeFromSuperview()
        window.addSubview(view)
    }
}
查看更多
一夜七次
5楼-- · 2020-03-03 10:24

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.

查看更多
登录 后发表回答