IOS - UIAppearanceWhenContainedIn Appearance

I set the image for my navigation bar as follows:

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

Then I do not want this image for the MFMessageComposeViewController classes, so I excluded it by doing the following:

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

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

+7
source share
3 answers

Found a solution to my problem:

Subclass MFMessageComposeViewController

In the init method, set the backgroundImage from navigationBar to nil

Voila!

 - (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; } 
+4
source

Before introducing the MFMessageComposeViewController try

 [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"MyUINavigationBarImageClear"] forBarMetrics:UIBarMetricsDefault]; 

and in messageComposeViewController: didFinishWithResult: callback reset to

 [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"MyUINavigationBarImageFull"] forBarMetrics:UIBarMetricsDefault]; 

I also set the .navigationBar.tintColor MFMessageComposeViewController property to get the cancel button according to my MyUINavigationBarImageClear image.

+2
source

Two ideas here (but not tested):

1) I suspect that trying to override with nil will not work - this statement is ignored. I suggest you create a transparent image and use it as a background for the MFMessageComposeViewController.

2) If this fails, I suspect that you will need to very accurately determine when to use your image, so you will have to replace the first statement with a long list of "when they are contained in" statements covering your entire class. if you have a subclass that all your view controllers use — some base class, then I believe you can use this instead. Hope # 1 works!

0
source

All Articles