在iOS 7上更改MFMailComposeViewController中的导航按钮颜色

时间:2013-10-12 11:48:53

标签: ios7 mfmailcomposeviewcontroller uiappearance

我正在尝试更改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上不起作用,看起来总是这样: enter image description here

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

navigationBar.tintColor = [UIColor redColor];

是否有更改iOS 7上MFMailComposeViewController中的导航按钮文本颜色?

6 个答案:

答案 0 :(得分: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]; 

答案 1 :(得分: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]];

答案 2 :(得分:3)

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

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

答案 3 :(得分:3)

如果您检查通过显示MFMailComposeViewController创建的视图层次结构,您将看到它包含在_UITextEffectsRemoteView的实例中。您对其中的任何子视图都没有编程访问权限,我猜这是因为它们可能由一个单独的进程拥有。这些子视图将继承在各种UIAppearance代理上设置的任何内容(例如,bar background,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

答案 4 :(得分:1)

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

答案 5 :(得分:1)

正如已经指出的那样,在MFMailComposeViewController导航栏上设置色调不起作用。如果您为整个应用程序设置了其他外观更改,则色调颜色只是问题的一个方面,在我们的应用程序中,我们更改了条形颜色和UIBarButton文本大小,因此在MFMailComposeViewController中我们看到: Navigation bar style in 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,这非常有效。