【问题标题】:MFMailComposeViewController bar background color not changing in iOS7MFMailComposeViewController 栏背景颜色在 iOS7 中没有改变
【发布时间】:2014-02-28 03:26:24
【问题描述】:

我正在尝试更改 iOS7MFMailComposeViewController 的背景颜色,但我无法成功。

我正在使用以下截图:

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 的背景颜色吗?

【问题讨论】:

    标签: ios objective-c ipad ios7


    【解决方案1】:

    这里的诀窍是调用“外观方法”,例如

    [UINavigationBar appearance].barTintColor = [UIColor whiteColor];
    [UINavigationBar appearance].tintColor = [UIColor redColor];
    

    致电之前

    [[MFMailComposeViewController alloc] init];
    

    这样配色方案将应用于邮件编写器。 它可能会在mailComposeController:didFinishWithResult:中恢复为默认值

    【讨论】:

    • 该死的BEFORE在这里很重要!
    • 对于 iOS 7.0 及更高版本,这似乎是正确的解决方案。此外,您可能需要在 init 之后调用相同的 barTintColor 和 tintColor 方法将它们设置回应用的原始颜色。
    • 这样做的缺点是您不能以这种方式更改 .Translucent 属性。
    • 一个好地方放在appDelegate的didFinishLaunchingWithOptions中
    • @DanielSpringer just let mc = MFMailComposeViewController()
    【解决方案2】:

    试试这个。为我工作。

    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。
    • 在调用邮件编写器的视图控制器中试试这个 [[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]];
    • 在 iOS 9.2.1 上,我测试了 [[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]];,但对我不起作用。我不得不使用[myMailViewController.navigationBar setTintColor:[UIColor whiteColor]];,它现在又可以在这个 iOS 版本上运行了。
    【解决方案3】:

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

    【讨论】:

    • 是否可以只将取消按钮着色为不同的颜色,比如红色,让发送按钮保持白色?
    • 使用 iOS 11.3,各种设备,这对我不起作用,但 SoftDesigner 的回答确实
    【解决方案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];
    

    【讨论】:

      【解决方案5】:

      试试这个,但一件事 BarTintColor 仅适用于 iOS7

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

      除非您将 translucent 属性设置为 NO,否则该颜色默认为半透明。

      或试试这个链接,它会对你更有帮助

      Changing MFMailComposeViewController's toolbar color

      【讨论】:

        【解决方案6】:

        试试下面的代码

            - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
        {
            [[UINavigationBar appearance] setBarTintColor:[UIColor blackColor]];
            [[UINavigationBar appearance] setBackgroundColor:[UIColor blackColor]];
        
            // Your usual code follows here ......
        

        【讨论】:

          【解决方案7】:

          @SoftDesigner 的回答:

          从 iOS 9 开始:

          [UINavigationBar appearance].tintColor = yourFavoriteColor;
          

          不适用于 MFMailComposeViewController。

          其余的答案都有效(我用过),但据我所知,您仍然坚持使用 Apple 的导航栏按钮颜色。

          希望这可以减轻其他人的焦虑。

          【讨论】:

            【解决方案8】:

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

            [self presentViewController:emailDialog animated:TRUE completion:nil];
            
            [[UINavigationBar appearance] setBackgroundImage:nil 
                                              forBarPosition:UIBarPositionTopAttached 
                                                  barMetrics:UIBarMetricsDefault];
            

            【讨论】:

            • 感谢您的回答,但我正在尝试更改背景颜色而不是背景图像...我尝试了 '[[UINavigationBar appearance] setBackgroundColor:READER_NAVIGATION_BAR_BACKGROUND_COLOR];'显示我的 emailDialog 后不起作用。
            • 使用 setTintColor 代替 BackgroundColor
            【解决方案9】:

            我遇到了一个问题,使我无法设置背景颜色。原来我在其他地方有一些其他代码将背景图像设置为 [UIImage new]。

            以下代码修复了它:

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

            【讨论】:

              猜你喜欢
              • 2018-06-13
              • 2014-08-03
              • 2013-10-10
              • 2023-03-22
              • 2021-12-22
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多