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个视图控制器子类。
编辑:看到这个答案,因为我愚蠢。
- UIImageJPEGRepresentation compressionQuality与其他编码器
- 具有返回值的iOS块
- 设置导航栏的色调颜色恢复到iOS 6的蓝色
- 获取错误“xargs”不被识别为内部或外部命令。 通过Windows运行iOS的appiumtesting
- Facebook分享对话框:权限错误 – OAuth“Facebook平台”“insufficient_scope(#200)权限错误”
- 如何在Xamarin iOS中绘制文本?
- 指定一个快速closures(等效块)到一个现有的使用桥接访问的objective-c块
- UIAlertController自定义字体不适用于iOS
- 使用Swift协议时发生短路