10

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?

paulrehkugler
  • 3,241
  • 24
  • 45

3 Answers3

21

Try using UITextAttributeTextColor instead of NSForegroundColorAttributeName.

Jonathan Arbogast
  • 9,620
  • 4
  • 35
  • 47
  • Man, I can't believe it was something this dumb. This is one of those enums that keeps going back and forth through the iOS releases; I can't keep it straight in my head. – paulrehkugler Nov 08 '13 at 20:54
  • Oh dear Lord, thank you Jonathan!! 20 person-hours of debugging solved with this. – Marc Cenedella Feb 04 '14 at 22:15
  • I'd like to add that the same problem appears with QLPreviewController. It crashes when using UIAppearance and NSForegroundColorAttributeName. Using the now deprecated UITextAttributeTextColor works fine. – Yanik May 01 '14 at 13:22
  • my problem seemed to be a combination of this and changing `NSFontAttributeName` to `UITextAttributeFont` as well – Fonix Jun 03 '14 at 08:59
  • 1
    It seems iOS 8 bug, but no luck even with the above solution. – Mazen Kasser Nov 10 '14 at 03:23
  • The compiler will not allow this in iOS8. – Eric Alford Nov 11 '14 at 19:40
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.

c0ming
  • 3,407
  • 1
  • 21
  • 26
0

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.

Community
  • 1
  • 1
paulrehkugler
  • 3,241
  • 24
  • 45