Change title of MFMailComposeViewController
Asked Answered
C

6

14

I'm using MFMailComposeViewController for in-app email in my app, but I'm not able to change the title. As default it's showing the subject in the title, but I would like to set the title to be something else. How can I do that?

I've tried:

controller.title = @"Feedback";

but it didn't work.

Here's my code:

- (IBAction)email {
    NSArray *array = [[NSArray alloc] initWithObjects:@"[email protected]", nil];
    MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
    [[controller navigationBar] setTintColor:[UIColor colorWithRed:0.36 green:0.09 blue:0.39 alpha:1.00]];
    controller.mailComposeDelegate = self;
    controller.title = @"Feedback";
    [controller setSubject:@"Long subject"];
    [controller setMessageBody:@""
                        isHTML:NO];
    [controller setToRecipients:array];
    [self presentModalViewController:controller animated:YES];
    [controller release];
    [array release];
}

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
    [self becomeFirstResponder];
    [self dismissModalViewControllerAnimated:YES];
}
Cashbox answered 15/11, 2009 at 15:34 Comment(0)
A
21

You can set a different title for your MFMailComposeViewController with a single line, like so.

...
[self presentModalViewController:controller animated:YES]; // Existing line
[[[[controller viewControllers] lastObject] navigationItem] setTitle:@"SomethingElse"];
...

However, this implementation effectively relies on undocumented features of MFMailComposeViewController. You're accessing the navigationItem of a private class (_MFMailComposeRootViewController) and changing its title to something other than the mail subject. I echo Art Gillespie's sentiment in that you should not do this and are very likely to be rejected by the Apple reviewers for doing something like this. In addition, this process could change completely in any minor point release of the iPhone OS, possibly causing crashes for your users until you can release an update to fix the behavior.

The decision is up to you, though, and if you still want to take these unrecommended steps, that is how you do it.

Alcott answered 23/11, 2009 at 20:1 Comment(3)
Thank you, just wanted to know how to something like that. After talking with some more people, I've decided to change my subject of the email to something that fits. Anyway, thanks :)Cashbox
could be written as controller.topViewController.navigationItem.title = @"SomethingElse";Viipuri
This solution isn't working for me on iOS8 - the title stays putBorlow
A
14

From the MFMailComposeViewController Class Reference:

Important: The mail composition interface itself is not customizable and must not be modified by your application. In addition, after presenting the interface, your application is not allowed to make further changes to the email content.

Adrianaadriane answered 15/11, 2009 at 16:20 Comment(3)
I know, but my question wasn't if it was allowed, my question was; is it possible? and how? ;-)Cashbox
This is still a relevant response to your question, as using private APIs on the iPhone is highly discouraged. Even if it is possible, it's worthwhile to know that this is not the suggested course of action.Chambray
Ok, maybe your right, but I still like to know how to do it :) ?Cashbox
Q
3

It seems that the subject of the message feeds the title of MFMailComposeViewController in iOS 8.

Quechua answered 22/6, 2015 at 16:25 Comment(0)
R
2

You should be able to take just a view (controller.view) and place it inside of your controller ... in that moment, you are not modifying anything and you are actually doing almost the same thing like Apple in their iPad email app when composing an email ... same thing should work on iPhone too ...

Rambutan answered 29/10, 2010 at 14:45 Comment(0)
G
2

Sbrocket's answer works great. This is how to add a title view (label):

// existing
[self presentModalViewController:controller animated:YES];

// new code
CGRect frame = CGRectMake(0, 0, 320, 44);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont fontWithName:@"MarkerFelt-Thin" size:18.0];
label.adjustsFontSizeToFitWidth = YES;
label.minimumFontSize = 12.0;
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor darkGrayColor];
label.text = @"Your Comments";
[[[[controller viewControllers] lastObject] navigationItem] setTitleView:label];

Same comments as above, it's not really recommended to customize MFMailComposeViewController...

Glutinous answered 17/6, 2012 at 8:52 Comment(0)
F
0

Most likely, you would have to dig down in the view hierarchy to find the raw UINavigationBar that contains the title, and manually set the title on that.

The program class-dump may come in handy here for determining the exact classes used. Trial, error, and a debugger are most likely your best bet.

Falter answered 20/11, 2009 at 7:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.