24

I'm trying to change the background color of the MFMailComposeViewController in iOS7 but I cannot make it work.

I'm using the following snipped:

MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;

if([picker.navigationBar respondsToSelector:@selector(barTintColor)]) {
    // iOS7
    picker.navigationBar.barTintColor = READER_NAVIGATION_BAR_BACKGROUND_COLOR;
    // Set back button arrow color
    [picker.navigationBar setTintColor:READER_NAVIGATION_BAR_BACK_BUTTON_ARROW_COLOR];

    // Set Navigation Bar Title Color
    [picker.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObject:READER_NAVIGATION_BAR_TITLE_NORMAL_FONT_COLOR forKey:UITextAttributeTextColor]];

    // Set back button color
    [[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:READER_NAVIGATION_BAR_BUTTONS_FONT_COLOR, UITextAttributeTextColor,nil] forState:UIControlStateNormal];

} 

Does anybody knows how to change the bakcground color of the MFMailComposeViewController in iOS7?

Manuel Escrig
  • 2,825
  • 1
  • 27
  • 36

9 Answers9

71

The trick here is to call 'appearance methods' such as

[UINavigationBar appearance].barTintColor = [UIColor whiteColor];
[UINavigationBar appearance].tintColor = [UIColor redColor];

BEFORE calling to

[[MFMailComposeViewController alloc] init];

This way the color scheme will be applied to the mail composer. It may be returned back to defaults in mailComposeController:didFinishWithResult:

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
SoftDesigner
  • 5,640
  • 3
  • 58
  • 47
34

try this. worked for me.

MFMailComposeViewController* myailViewController = [[MFMailComposeViewController alloc] init];
// set other attributes of mailcomposer here.
myMailViewController.mailComposeDelegate = self;

[myMailViewController.navigationBar setTintColor:[UIColor whiteColor]];

[self presentViewController:myMmailViewController animated:YES completion:nil];
gaurish.salunke
  • 1,059
  • 10
  • 18
  • Thanks for your answer but this doesn't work neither... it does work for iOS6 but not for iOS7 which is what I'm looking for. – Manuel Escrig Feb 04 '14 at 09:02
  • 6
    try this in the viewcontroller where your calling the mail composer [[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]]; – gaurish.salunke Feb 04 '14 at 09:04
  • 1
    On iOS 9.2.1 I tested `[[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]];` which didn't work for me. I had to use `[myMailViewController.navigationBar setTintColor:[UIColor whiteColor]];` which now is working again on this iOS version. – Manfred Scheiner Jan 17 '16 at 12:22
16

Swift 3 solution:

extension MFMailComposeViewController {
    override open func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        UIApplication.shared.statusBarStyle = UIStatusBarStyle.lightContent
    }

    open override func viewDidLoad() {
        super.viewDidLoad()
        navigationBar.isTranslucent = false
        navigationBar.isOpaque = false
        navigationBar.barTintColor = UIColor.white
        navigationBar.tintColor = UIColor.white
    }
}
mazorati
  • 2,031
  • 3
  • 22
  • 22
  • Is it possible to colorize only the cancel button a different color, say red and have the send button stay the white color? – icekomo Jan 04 '17 at 06:07
  • Using iOS 11.3, various devices, this didn't work for me, but SoftDesigner's answer did – erparker Apr 20 '18 at 22:15
4

For iOS8:

NSDictionary *navbarTitleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                            [UIColor whiteColor],UITextAttributeTextColor, 
                                            [UIColor blackColor], UITextAttributeTextShadowColor, 
                                            [NSValue valueWithUIOffset:UIOffsetMake(-1, 0)], UITextAttributeTextShadowOffset, nil];

[[UINavigationBar appearance] setTitleTextAttributes:navbarTitleTextAttributes];

Or

navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor yellowColor] forKey:UITextAttributeTextColor];
1

try this but one thing BarTintColor available only iOS7

[[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];

This color is made translucent by default unless you set the translucent property to NO.

or try this link it will more helpful you

Changing MFMailComposeViewController's toolbar color

Community
  • 1
  • 1
codercat
  • 22,873
  • 9
  • 61
  • 85
1

Try the following code

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[UINavigationBar appearance] setBarTintColor:[UIColor blackColor]];
    [[UINavigationBar appearance] setBackgroundColor:[UIColor blackColor]];

    // Your usual code follows here ......
Rajesh
  • 850
  • 9
  • 18
1

@SoftDesigner's answer:

As of iOS 9:

[UINavigationBar appearance].tintColor = yourFavoriteColor;

does not work on the MFMailComposeViewController.

The rest of the answer works (I used it), but as far as I can tell you're stuck with Apple's colors for the nav bar buttons.

Hope this saves someone else some angst.

Peter
  • 201
  • 2
  • 4
0

First present the MFMailComposeViewController then change its tintColor

[self presentViewController:emailDialog animated:TRUE completion:nil];

[[UINavigationBar appearance] setBackgroundImage:nil 
                                  forBarPosition:UIBarPositionTopAttached 
                                      barMetrics:UIBarMetricsDefault];
Alex Cio
  • 6,014
  • 5
  • 44
  • 74
Sunny Shah
  • 12,990
  • 9
  • 50
  • 86
  • Thanks for your answer but I'm trying to change the background color not the background image... I tried ' [[UINavigationBar appearance] setBackgroundColor:READER_NAVIGATION_BAR_BACKGROUND_COLOR];' after showing my emailDialog and doesn't work. – Manuel Escrig Feb 04 '14 at 09:00
  • use setTintColor instead of BackgroundColor – Sunny Shah Feb 04 '14 at 09:05
0

I had a problem that prevented me from setting the background colour. Turns out I had some other code elsewhere setting the background image to [UIImage new].

The following code fixed it:

 [[UINavigationBar appearance] setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
 [[UINavigationBar appearance] setShadowImage:nil];
Chris Birch
  • 2,041
  • 2
  • 19
  • 22