19

我正在尝试更改 MFMailComposerViewController 中导航按钮的文本颜色,但它不像在 iOS 6 上那样工作。在 iOS 6 中,它与 UIAppearance 一起工作,如下所示:

// Navigation button
UIBarButtonItem *barButton = [UIBarButtonItem appearance];
NSDictionary *barButtonTitleTextAttributes = @{UITextAttributeTextColor: [UIColor redColor]};
NSDictionary *disabledBarButtonTitleTextAttributes = @{UITextAttributeTextColor: [UIColor grayColor]};

[barButton setTitleTextAttributes:barButtonTitleTextAttributes forState:UIControlStateNormal];
[barButton setTitleTextAttributes:disabledBarButtonTitleTextAttributes forState:UIControlStateDisabled];
[barButton setBackgroundImage:[[UIImage imageNamed:@"btn_appearance"] stretchableImageWithLeftCapWidth:6 topCapHeight:0] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

但这在 iOS 7 上不起作用,并且看起来总是这样: 在此处输入图像描述

我也尝试tintColor在 navigationBar 上设置属性,但这也没有效果:

navigationBar.tintColor = [UIColor redColor];

无论如何在 iOS 7 上更改 MFMailComposeViewController 中的导航按钮文本颜色?

4

6 回答 6

35

我使用了这个并且在 iOS7+ 中完美运行

MFMailComposeViewController* mailViewController = [[MFMailComposeViewController alloc] init];        
mailViewController.mailComposeDelegate = self;
[mailViewController setToRecipients:@[@"email@apple.com"]];

[mailViewController.navigationBar setTintColor:[UIColor orangeColor]];

[self presentViewController:mailViewController animated:YES completion:nil]; 
于 2014-01-17T10:24:14.170 回答
4

就像 OemerA 说的 - 没有办法改变颜色。我的问题是,在 UIAppearance 中,我将栏的背景颜色设置为蓝色,因此“按钮”不再可见。由于电子邮件实际上不是您的应用程序的一部分,因此在创建邮件编写器之前重置导航栏外观更有意义。我就是这样做的:

// set to normal white
[[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]];
[[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor], NSForegroundColorAttributeName, nil]];

MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];

// set to back to blue with white text
[[UINavigationBar appearance] setBarTintColor:[UIColor blueColor]];
[[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil]];
于 2013-12-19T04:02:48.363 回答
3

如果您在UIWindow上设置tintColor它工作得很好,第一次呈现 MFMailComposerViewController。似乎它丢失了后续调用的tintColor信息。

注意:这会改变窗口中每个元素的色调。

于 2013-10-19T14:16:52.540 回答
3

如果您检查通过显示 MFMailComposeViewController 创建的视图层次结构,您会看到它包含在 _UITextEffectsRemoteView 的实例中。您对任何子视图的编程访问权限为零,我猜这是因为它们可能由一个单独的进程拥有。这些子视图将继承在各种 UIAppearance 代理上设置的任何内容(例如,条形背景、titleTextAttributes 等),但仅此而已。

UIAppearance 协议在文档中没有提到这一点,但在头文件的注释中确实有这个:

Note for iOS7: On iOS7 the tintColor property has moved to UIView, and now has special inherited behavior described in UIView.h.
This inherited behavior can conflict with the appearance proxy, and therefore tintColor is now disallowed with the appearance proxy.

所以最终结果是,虽然您可以控制 MFMailComposeViewController 外观的大部分方面,但您将始终获得系统默认的蓝色色调。

错误报告: http://openradar.appspot.com/radar?id= 6166546539872256

于 2013-10-23T16:56:58.887 回答
2

正如已经多次指出的那样,在 MFMailComposeViewController 导航栏上设置色调不起作用。如果您为整个应用程序设置了其他外观更改,则色调只是问题的一个方面,在我们的应用程序中,我们更改了条形颜色和 UIBarButton 文本大小,因此在 MFMailComposeViewController 中我们看到: MFMailComposeViewController 中的导航栏样式

我们应用程序的外观设置在 StyleGuide 类中,该类具有configureAppearanceModifiers从 AppDelegate 调用的函数。

以@timosdk 为例,我添加了第二种方法:

- (void)neutraliseAppearanceModifiers {

    [[UINavigationBar appearance] setTranslucent:NO];
    [[UINavigationBar appearance] setTintColor:nil];
    [[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBackIndicatorImage:nil];
    [[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:nil];
    [[UINavigationBar appearance] setBackgroundImage:nil
                                  forBarPosition:UIBarPositionAny
                                      barMetrics:UIBarMetricsDefault];
    [[UIBarButtonItem appearance] setTitleTextAttributes:nil forState:UIControlStateNormal];
    [[UIBarButtonItem appearance] setTitleTextAttributes:nil forState:UIControlStateHighlighted];
    [[UIBarButtonItem appearance] setTitleTextAttributes:nil forState:UIControlStateDisabled];
    [[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:nil forState:UIControlStateNormal];
    [[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:nil forState:UIControlStateHighlighted];
    [[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:nil forState:UIControlStateDisabled];

}

我在初始化 MFMailComposeViewController 之前调用它,然后在关闭 ViewController 之前configureAppearanceModifiers再次调用委托,这非常有效。didFinishWithResult

于 2017-10-04T10:43:45.047 回答
1

斯威夫特 3.0

func sendEmail() {
    if MFMailComposeViewController.canSendMail() {
        let mail = MFMailComposeViewController()
        mail.navigationBar.tintColor = UIColor.red
        mail.mailComposeDelegate = self
        mail.setToRecipients(["abc@abc.com"])
        mail.setMessageBody("<p>You're so awesome!</p>", isHTML: true)

        present(mail, animated: true)
    } else {
        // show failure alert
    }
}

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
    controller.dismiss(animated: true)
}
于 2017-08-21T06:08:11.680 回答