How to change buttons in MFMailComposeViewController?
Asked Answered
B

3

12

In my app I am using the MFMailComposeViewController. I have placed a back and Done button in the title bar. I have the title bar to be in black color, but I have the button background in blue color. How to change the button background color to black in color?

Barbabas answered 26/8, 2011 at 13:38 Comment(0)
H
17

You first have to change the button style: barButton.style = UIBarButtonItemStyleBordered;

Afterwards, the color of the navigation bar buttons can be changed with the following code:

[[mailComposer navigationBar] setTintColor:[UIColor blackColor]];
Herzel answered 26/8, 2011 at 13:48 Comment(0)
C
4

I followed this to add custom buttons replacing the standard cancel and send buttons:

// Fetch the UINavigationItem object of the nav bar
UINavigationItem *mailVCNavItem = [mailVC.navigationBar.items objectAtIndex:0];

// Get the old bar button item to fetch the action and target.
UIBarButtonItem *oldCancelBarButton = [mailVCNavItem leftBarButtonItem];

// Create your new custom bar button item. 
// In my case I have UIButton with image set as a custom view within a bar button item.
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:[UIImage imageNamed:@"backButton.png"] forState:UIControlStateNormal];
[backButton addTarget:oldCancelBarButton.target action:oldCancelBarButton.action forControlEvents:UIControlEventTouchUpInside];
backButton.bounds = CGRectMake(0.0, 0.0, 40.0, 25.0);
[[barButtonItems objectAtIndex:0] setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:backButton]];

Unfortunately I wasn't able to replace the Send button.

It just renders the button useless.

Corsica answered 31/7, 2012 at 16:3 Comment(1)
Doesn't work for me in iOS7 but looks sensible code.Higgler
C
1

For Swift (I am using Swift 1.2)

var mc: MFMailComposeViewController = MFMailComposeViewController()
mc.mailComposeDelegate = self
mc.setSubject(emailTitle)
mc.setToRecipients(toRecipients)
mc.navigationBar.tintColor = UIColor.blackColor()
Cabbagehead answered 25/8, 2015 at 17:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.