更改MFMailComposeViewController中导航栏的主标题颜色

15

我在普通的ViewController上没有更改导航栏主标题颜色的问题,但是在MFMailComposeViewController上是不可能的。 我可以更改按钮的颜色(取消和发送),我可以设置导航栏的背景,但是无法更改标题的颜色。我不想设置一个新的标题(显然,苹果不允许这样做),我只想改变颜色 :'(

请帮助我。 谢谢


请参考此链接:https://dev59.com/FnI-5IYBdhLWcg3w3crS - Dee
4个回答

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

希望这对您有用。


4
此应用程序中颜色会随着操作而变化,但在MFMailCompose中不会变化。IOS 12。 - rptwsthi

15

这是适用于iOS 7、8、9和10的正确答案:

MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[[picker navigationBar] setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor blackColor] forKey:NSForegroundColorAttributeName]];

为什么要这样做:

上面被打钩的回答(由Mani提供)参考了[UINavigationBar appearance],它是不正确的,因为它会改变弹出MFMailComposeViewControllerUINavigationBar中标题的颜色,而我不想让它发生。你需要像我的代码一样明确地获取选择器的NavBar。

在iOS 7中(由Mani另一个回答提供),设置tintColor也是不正确的,因为它设置按钮的颜色,而不是标题。

此外,UITextAttributeTextColor现在已经弃用,请使用NSForegroundColorAttributeName


10
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc]init];
    picker.mailComposeDelegate = self;
    [[picker navigationBar] setTintColor:[UIColor blackColor]];

这不是我要找的,这里只能改变导航栏的颜色(我已经有一个背景了),我想要改变标题视图的颜色。 - user1451163

0

对于除黑色以外的颜色,请尝试使用以下代码:

MFMailComposeViewController *mailController  = [MFMailComposeViewController new];

            [mailController.navigationBar setTintColor:[UIColor colorWithHue:240.0f/359.0f
                                                                  saturation:85.0f/100.0f 
                                                                  brightness:60.0f/100.0f 
                                                                       alpha:0.0f]];

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