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

时间:2013-11-08 16:28:45

标签: ios objective-c mfmailcomposeviewcontroller uiappearance

当我提出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:方法中将其跟踪到以下外观设置:

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

注释掉该线路可以解决问题,但会破坏应用的其余部分,所以我尝试将titleTextAttributes专门设置为MFMailComposeViewController的空字典:

尝试#1

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

导致同样的崩溃。和

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

也导致同样的崩溃。

尝试#2

我注意到MFMailComposeViewControllerUINavigationController,因此全局外观设置可能已本地化为UIVavigationController内的 UIVavControllers 。我整理了一些代码来确定MFMailComposeViewController中的视图控制器:

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

导致输出结果:

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

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

        [[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:

的完成块中恢复全局外观属性

然而,这种方法也不起作用。有没有人知道如何在全局UINavigationBar外观上设置titleTextAttributes而不会崩溃MFMailComposeViewController?

3 个答案:

答案 0 :(得分:21)

尝试使用UITextAttributeTextColor代替NSForegroundColorAttributeName

答案 1 :(得分:2)

只需扩展UINavigationController类

@interface MyNavigationController : UINavigationController
@end

用新的子类替换所有UINavigationController类 和appwate中的[appearanceWhenContainedIn:]代理

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

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

答案 2 :(得分:0)

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

编辑:请参阅this answer,因为我很蠢。