我在普通的ViewController上没有更改导航栏主标题颜色的问题,但是在MFMailComposeViewController上是不可能的。 我可以更改按钮的颜色(取消和发送),我可以设置导航栏的背景,但是无法更改标题的颜色。我不想设置一个新的标题(显然,苹果不允许这样做),我只想改变颜色 :'(
请帮助我。 谢谢
我在普通的ViewController上没有更改导航栏主标题颜色的问题,但是在MFMailComposeViewController上是不可能的。 我可以更改按钮的颜色(取消和发送),我可以设置导航栏的背景,但是无法更改标题的颜色。我不想设置一个新的标题(显然,苹果不允许这样做),我只想改变颜色 :'(
请帮助我。 谢谢
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];
希望这对您有用。
这是适用于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]
,它是不正确的,因为它会改变弹出MFMailComposeViewController
的UINavigationBar
中标题的颜色,而我不想让它发生。你需要像我的代码一样明确地获取选择器的NavBar。
在iOS 7中(由Mani另一个回答提供),设置tintColor
也是不正确的,因为它设置按钮的颜色,而不是标题。
此外,UITextAttributeTextColor
现在已经弃用,请使用NSForegroundColorAttributeName
。
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc]init];
picker.mailComposeDelegate = self;
[[picker navigationBar] setTintColor:[UIColor blackColor]];
对于除黑色以外的颜色,请尝试使用以下代码:
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]];