10

当我提出 a 时,我遇到了以下崩溃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

我注意到这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

所以我尝试了(即使依赖 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 崩溃?

4

3 回答 3

21

尝试使用UITextAttributeTextColor而不是NSForegroundColorAttributeName.

于 2013-11-08T19:52:58.350 回答
2

Just extends UINavigationController class

@interface MyNavigationController : UINavigationController
@end

replace all your UINavigationController class with the new subclass and [appearanceWhenContainedIn:] in your app delegate

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

after that your app will not crash.

于 2014-01-15T06:05:24.390 回答
0

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

编辑:看到这个答案,因为我很笨。

于 2013-11-08T19:38:08.043 回答