MFMailComposeViewController在iOS7中的导航栏背景颜色无法改变

24

我正在尝试在iOS7中更改MFMailComposeViewController的背景颜色,但我无法使其起作用。

我正在使用以下代码段:

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];

} 

有人知道如何在iOS7中更改MFMailComposeViewController的背景颜色吗?

9个回答

71
这里的技巧是调用“外观方法”,例如:
[UINavigationBar appearance].barTintColor = [UIColor whiteColor];
[UINavigationBar appearance].tintColor = [UIColor redColor];

在致电之前

[[MFMailComposeViewController alloc] init];

通过这种方式,配色方案将应用于邮件撰写器。mailComposeController:didFinishWithResult:中可以将其返回到默认设置。


20
该句话需要上下文才能确定最准确的翻译,但是从该句话本身来看,它的意思是“之前的事情非常重要”。 - Sjoerd Perfors
1
对于iOS 7.0及以上版本,似乎这是正确的解决方案。此外,在初始化之后,您可能需要调用相同的barTintColor和tintColor方法将它们恢复为应用程序的原始颜色。 - Nick N
这样做的缺点是你无法通过这种方式更改.Translucent属性。 - scord
1
一个好的放置位置是在 appDelegate 的 didFinishLaunchingWithOptions 方法中。 - MobileMon

34

试一试,对我有效。

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];

谢谢你的回答,但这个方法也不行...它可以在iOS6上运行,但我需要的是在iOS7上运行。 - Manuel Escrig
6
在调用邮件组件的视图控制器中尝试使用以下代码:[[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]]; 这行代码可以设置导航栏的背景颜色为白色。 - gaurish.salunke
1
在iOS 9.2.1上,我测试了[[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]];但是它对我没有起作用。我不得不使用[myMailViewController.navigationBar setTintColor:[UIColor whiteColor]];现在在这个iOS版本上又可以工作了。 - Manfred Scheiner

16

Swift 3的解决方案:

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

是否可以仅将取消按钮着色为不同的颜色,比如红色,并使发送按钮保持白色? - icekomo
使用iOS 11.3和不同的设备,这对我来说并没有起作用,但是SoftDesigner的答案有用。 - erparker

4

iOS8:

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

[[UINavigationBar appearance] setTitleTextAttributes:navbarTitleTextAttributes];

或者

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

1

@SoftDesigner的回答:

截至iOS 9:

[UINavigationBar appearance].tintColor = yourFavoriteColor;

在MFMailComposeViewController上无法使用。

其余的答案可行(我已经使用了它),但据我所知,你只能使用苹果为导航栏按钮设置的颜色。

希望这能帮助其他人避免一些烦恼。


1

尝试这个,但是需要注意 BarTintColor 只适用于 iOS7。

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

默认情况下,此颜色会变得半透明,除非您将半透明属性设置为NO。
或者尝试此链接,它会更有帮助:
更改MFMailComposeViewController工具栏颜色

1
尝试以下代码。
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[UINavigationBar appearance] setBarTintColor:[UIColor blackColor]];
    [[UINavigationBar appearance] setBackgroundColor:[UIColor blackColor]];

    // Your usual code follows here ......

0

我遇到一个问题,无法设置背景颜色。结果发现我在别的地方有一些代码将背景图像设置为[UIImage new]。

以下代码解决了这个问题:

 [[UINavigationBar appearance] setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
 [[UINavigationBar appearance] setShadowImage:nil];

0

首先展示MFMailComposeViewController,然后更改其tintColor

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

[[UINavigationBar appearance] setBackgroundImage:nil 
                                  forBarPosition:UIBarPositionTopAttached 
                                      barMetrics:UIBarMetricsDefault];

谢谢您的回答,但我想要更改背景颜色而不是背景图片...我尝试在显示我的emailDialog后使用“[[UINavigationBar appearance] setBackgroundColor:READER_NAVIGATION_BAR_BACKGROUND_COLOR];”,但没有效果。 - Manuel Escrig
使用setTintColor而不是BackgroundColor - Sunny Shah

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接