问题
I am getting the following crash when I present 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'
I've tracked it down to the following appearance setting in my AppDelegate's application:didFinishLaunchingWithOptions:
method:
[[UINavigationBar appearance] setTitleTextAttributes:
@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
Commenting that line out does the trick, but ruins the rest of the app, so I tried specifically setting the titleTextAttributes to an empty dictionary for the MFMailComposeViewController
:
Attempt #1
[[UINavigationBar appearanceWhenContainedIn:
NSClassFromString(@"MFMailComposeViewController"), nil]
setTitleTextAttributes:@{ }];
That results in the same crash. And
[[UINavigationBar appearanceWhenContainedIn:
NSClassFromString(@"MFMailComposeViewController"), nil]
setTitleTextAttributes:nil];
also results in the same crash.
Attempt #2
I noticed that MFMailComposeViewController
is a UINavigationController
, so maybe the global appearance settings are localized to UIViewControllers inside a UINavigationController. I put together some code to figure out what view controllers are inside the MFMailComposeViewController:
for (UIViewController *viewController in mailViewController.viewControllers) {
NSLog(@"%@", NSStringFromClass([viewController class]));
}
Which results in the output:
2013-11-08 11:04:05.936 <redacted>[7108:907] MFMailComposeInternalViewController
So I tried (even though it's bad practice to rely on Apple's private view controllers):
[[UINavigationBar appearanceWhenContainedIn:
NSClassFromString(@"MFMailComposeViewController"), nil]
setTitleTextAttributes:@{ }];
And
[[UINavigationBar appearanceWhenContainedIn:
NSClassFromString(@"MFMailComposeViewController"), nil]
setTitleTextAttributes:nil];
But that still results in the same crash!
Attempt #3
// right before instantiating the MFMailComposeViewController
[[UINavigationBar appearance] setTitleTextAttributes:@{ }];
And
[[UINavigationBar appearance] setTitleTextAttributes:nil];
Then restoring the global appearance properties in the completion block of dismissViewController:animated:completion:
However, this approach didn't work either. Does anyone know how to set titleTextAttributes on the global UINavigationBar
appearance without crashing MFMailComposeViewController?
回答1:
Try using UITextAttributeTextColor
instead of NSForegroundColorAttributeName
.
回答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.
回答3:
The only way I was able to solve this was to create [[UINavigationBar appearanceWhenContainedIn:] setTitleTextAttributes:]
for each of my UIViewControllers
. Luckily this was fairly simple, because all of my custom view controllers come from 4 view controller subclasses.
Edit: see this answer because I'm dumb.
来源:https://stackoverflow.com/questions/19863972/mfmailcomposeviewcontroller-crashes-because-of-global-appearance-properties-on-i