Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
342 views
in Technique[技术] by (71.8m points)

ios - MFMailComposeViewController Crashes because of Global Appearance Properties on iOS6

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try using UITextAttributeTextColor instead of NSForegroundColorAttributeName.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...