Search code examples
iosuiappearance

Using UIAppearance and switching themes


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?


Solution

  • 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.