iOS – UIAppearance appearanceWhenContainedIn的问题

我正在为我的导航栏设置一个图像,如下所示:

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

然后我不想这个图像MFMessageComposeViewController类,所以我排除它这样做:

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

但它没有任何作用(导航栏仍然是我的图像在MFMessageComposeViewController里面)。 我在这里错过了什么?

find解决我的问题的方法:

子类MFMessageComposeViewController

在init方法中,将navigationBarbackgroundImage设置为nil

瞧!

 - (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; } 

就在展示MFMessageComposeViewController之前试试

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

并在messageComposeViewController:didFinishWithResult:callback重置为

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

我还设置MFMessageComposeViewController的.navigationBar.tintColor属性以获得取消button以匹配我的MyUINavigationBarImageClear图像。

这里有两个想法(但没有testing):

1)我怀疑试图用nil覆盖是不行的 – 该语句被忽略。 我build议你做的是创build一个透明的图像,并将其作为MFMessageComposeViewController的背景。

2)如果失败了,那么我怀疑必须非常具体地说明何时使用你的形象,所以你将不得不用第一个陈述来replace一个包含你全class的陈述。 如果你有一个所有的视图控制器使用的子类 – 一些基类 – 那么我相信你可以使用它。 希望#1的作品!