Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override UIAppearance property for MFMailComposeViewController

I am using the UIAppearance protocol to set the background image of UINavigationBar objects throughout my app.

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"image-name"] forBarMetrics:UIBarMetricsDefault];

I would like to override this for instances of MFMailComposeViewController so that the default style navigation bar is displayed. I attempted to use appearanceWhenContainedIn to set this and this works on iOS 5 but not on iOS 6.

[[UINavigationBar appearanceWhenContainedIn:[MFMailComposeViewController class], nil] setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];

Am I making an error or is there a better way to accomplish this?

like image 891
markdorison Avatar asked Mar 22 '13 21:03

markdorison


3 Answers

Changing the appearance of a MFMailComposer through normal measures is not possible, but there is a little workaround you can do, which I've used many times before.

Add two methods to the class in which you wish to implement the new look to:

- (void)applyComposerInterfaceAppearance
{
    [[UINavigationBar appearance] setTintColor:[UIColor blueColor]];
}

- (void)applyGlobalInterfaceAppearance
{
    // My default color of choice
    [[UINavigationBar appearance] setTintColor:[UIColor redColor]];
}

Now in your show method, apply the special composer interface changes you'd like to make.

- (void)showMailComposer
{
    if ([MFMailComposeViewController canSendMail]) 
    {
        [self applyComposerInterfaceApperance];

        MFMailComposeViewController *viewController = [[MFMailComposeViewController alloc] init];
        viewController.mailComposeDelegate = delegate;
        [viewController setToRecipients:mailRecepients];
        [viewController setSubject:mailSubject];
        [viewController setMessageBody:messageBody isHTML:NO];
        [self presentModalViewController:viewController animated:YES];
    }
}

And in your delegate, change the interface back to the way it was.

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    // Do normal mail composer did finish stuff in here
    [self applyGlobalInterfaceAppearance];
}
like image 182
mergesort Avatar answered Nov 01 '22 10:11

mergesort


The Mail Composer view is run in a different process under iOS 6 and cannot be tampered with directly (since the view is essentially inside another app). You cannot customize what it shows, it's the same for the Twitter & Facebook views.

Here is a more detailed description of remote view controllers: http://oleb.net/blog/2012/10/remote-view-controllers-in-ios-6/

like image 2
Hampus Nilsson Avatar answered Nov 01 '22 10:11

Hampus Nilsson


Simply set the tintColor on the MFMailComposeViewController instance:

[mailInstance.navigationBar setTintColor:[UIColor someColor]];
like image 1
smileBot Avatar answered Nov 01 '22 10:11

smileBot