MFMailComposeViewController由于iOS6上的全局外观属性而崩溃

当我介绍一个MFMailComposeViewController时,我得到以下崩溃:

 2013-11-08 11:04:05.963 <redacted>[7108:1603] *** Assertion failure in NSDictionary *_UIRecordArgumentOfInvocationAtIndex(NSInvocation *, NSUInteger, BOOL)(), /SourceCache/UIKit/UIKit-2380.17/UIAppearance.m:1118 2013-11-08 11:04:06.032 <redacted>[7108:1603] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Unknown key, "NSColor" in title text attributes dictionary' 

我在AppDelegate的application:didFinishLaunchingWithOptions:追踪了下面的外观设置application:didFinishLaunchingWithOptions: method:

  [[UINavigationBar appearance] setTitleTextAttributes: @{NSForegroundColorAttributeName : [UIColor whiteColor]}]; 

注释该行出了窍门,但遗失了应用程序的其余部分,所以我试着将titleTextAttributes设置为MFMailComposeViewController的空字典:

尝试#1

  [[UINavigationBar appearanceWhenContainedIn: NSClassFromString(@"MFMailComposeViewController"), nil] setTitleTextAttributes:@{ }]; 

这导致相同的崩溃。 和

  [[UINavigationBar appearanceWhenContainedIn: NSClassFromString(@"MFMailComposeViewController"), nil] setTitleTextAttributes:nil]; 

也导致相同的崩溃。

尝试#2

我注意到MFMailComposeViewController是一个UINavigationController ,所以也许全局的外观设置本地化到UINavigationController中的UIViewControllers。 我把一些代码放在一起来找出MFMailComposeViewController里面的视图控制器:

  for (UIViewController *viewController in mailViewController.viewControllers) { NSLog(@"%@", NSStringFromClass([viewController class])); } 

其结果是输出:

 2013-11-08 11:04:05.936 <redacted>[7108:907] MFMailComposeInternalViewController 

所以我尝试了(即使依靠苹果的私人视图控制器是不好的做法)

  [[UINavigationBar appearanceWhenContainedIn: NSClassFromString(@"MFMailComposeViewController"), nil] setTitleTextAttributes:@{ }]; 

  [[UINavigationBar appearanceWhenContainedIn: NSClassFromString(@"MFMailComposeViewController"), nil] setTitleTextAttributes:nil]; 

但是,这仍然导致同样的崩溃!

尝试#3

  // right before instantiating the MFMailComposeViewController [[UINavigationBar appearance] setTitleTextAttributes:@{ }]; 

  [[UINavigationBar appearance] setTitleTextAttributes:nil]; 

然后在dismissViewController:animated:completion:的完成块中恢复全局外观属性dismissViewController:animated:completion:

但是,这种方法也没有工作。 有谁知道如何设置全局UINavigationBar外观titleTextAttributes而不会崩溃MFMailComposeViewController?

尝试使用UITextAttributeTextColor而不是NSForegroundColorAttributeName

只是扩展UINavigationController类

 @interface MyNavigationController : UINavigationController @end 

用您的应用程序委托中的新子类和[appearanceWhenContainedIn:]replace所有的UINavigationController类

 [UINavigationBar appearanceWhenContainedIn:[MyNavigationController class], nil].titleTextAttributes = @{ NSForegroundColorAttributeName : [UIColor whiteColor] }; 

之后,你的应用程序不会崩溃。

我能解决这个问题的唯一方法是为每个UIViewControllers创build[[UINavigationBar appearanceWhenContainedIn:] setTitleTextAttributes:] 。 幸运的是,这很简单,因为我所有的自定义视图控制器都来自4个视图控制器子类。

编辑:看到这个答案,因为我愚蠢。