views:

1631

answers:

6

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];
}
+6  A: 

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.

Art Gillespie
I know, but my question wasn't if it was allowed, my question was; is it possible? and how? ;-)
mofle
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.
Brad Larson
Ok, maybe your right, but I still like to know how to do it :) ?
mofle
A: 

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.

chpwn
+1  A: 

I'm certainly not recommending that you do this in a real application (and Apple will likely reject it anyway), but you can add a subview to the MFMailComposeViewController instance's view. For example:

- (IBAction)email {
    MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];

    ...your code...

    UIView *evilView = [[UIView alloc] initWithFrame:CGRectMake(70, 20, 190, 44)];
    evilView.backgroundColor = [UIColor blueColor]; // TODO: custom drawing instead
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:evilView.bounds];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.text = @"Custom";
    titleLabel.textAlignment = UITextAlignmentCenter;
    titleLabel.textColor = [UIColor whiteColor];
    [evilView addSubview:titleLabel];
    [controller.view addSubview:evilView];
    [titleLabel release];
    [evilView release];

    [self presentModalViewController:controller animated:YES];
    [controller release];
}

In order to make this convincing, you'll need to modify evilView to contain the same gradient as the navigation bar. I'd probably use a UIImageView with an image that's a vertical slice of the navigation bar, stretched horizontally.

Sickpea
+3  A: 

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.

Sbrocket
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 :)
mofle
A: 

I actually found this post looking for a way to get rid of the title. I'm using a custom UINavigationBar on my NavigationController, the title goes over the top of the custom bar and looked horrible.

This solved the problem but I'd still like something more elegant (Apple kosher so to speak)

[[[[controller viewControllers] lastObject] navigationItem] setTitle:@""];

This is my customer UINavigationBar

@implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
    UIImage *image = [UIImage imageNamed: @"custom_header.png"];
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end

What I'd like to do is either display the MFMailComposeViewController in the view underneath the nav bar or simply display over the top of the Nav bar with an opaque nav bar. Any suggestions welcome.

This is the code I'm currently using but I'd prefer a better way to do display the MFMailComposeViewController

[picker setMessageBody:emailBody isHTML:YES];

[[self navigationController] presentModalViewController:picker animated:YES];
[[[[picker viewControllers] lastObject] navigationItem] setTitle:@""];

[picker release];   

Thanks in advance,

-John- @jtdavies

+1  A: 

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 ...

Ondrej