Search code examples
iosappdelegateuiappearance

Does UIAppearance work outside of AppDelegate?


I've played with UIAppearance a little bit, and it works fine. Currently I'm setting all the apperance-stuff in the AppDelegate, but now I need to do some customization after the App already started (Theming after a user logged in).

Is it possible to put the appearance-stuff outside of the AppDelegate? Of course I already tried, but it has no effect. Do I have to reload the wohle App-UI, and if so how can I do this?

Thank you!

Chris


Solution

  • If you are using xib to create your view you should do the appearance customization within the AppDelegate since customization should be performed before creating any UIControl objects. You can customize it for different view controllers/ containers using appearanceWhenContainedIn

    To customize the appearances for instances of a class contained within an instance of a container class, or instances in a hierarchy, use +appearanceWhenContainedIn: for the appropriate appearance proxy.

    For example:

    [[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTintColor:myNavBarColor];   
    [[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], [UIPopoverController class], nil] setTintColor:myPopoverNavBarColor];
    

    Use ViewController class

     [[UIStepper appearanceWhenContainedIn:[MainViewController class], nil]setTintColor:[UIColor redColor]];
     [[UIStepper appearanceWhenContainedIn:[DetailViewController class], nil]setTintColor:[UIColor greenColor]];  
    

    If you are creating the controls programmatically, no matter customize just before creating the control

     [[UIStepper appearance]setTintColor:[UIColor redColor]];
     UIStepper *stepper = [[UIStepper alloc]init];
     [self.view addSubview: stepper];