Search code examples
ioscocoa-touchuiappearance

iOS – UIAppearance appearanceWhenContainedIn issues


I'm setting an image for my navigationbar like so:

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navbar.png"] forBarMetrics:UIBarMetricsDefault];

Then I don't want this image for classes of MFMessageComposeViewController so I exclude it by doing this:

[[UINavigationBar appearanceWhenContainedIn:[MFMessageComposeViewController class], nil] setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];

But it has no effect (the navbar is still styled with my image inside the MFMessageComposeViewController). What am I missing here?


Solution

  • Found out the solution to my problem:

    Subclass the MFMessageComposeViewController

    In the init method set the backgroundImage of the navigationBar to nil

    Voilá!

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
        if (self) {
            // Custom initialization
            [self.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    
        }
        return self;
    }