【问题标题】:Change title color of navigation bar in MFMailComposeViewController in iOS 12 not working [duplicate]在 iOS 12 中更改 MFMailComposeViewController 中导航栏的标题颜色不起作用[重复]
【发布时间】:2018-09-26 16:26:18
【问题描述】:

如何在iOS 12中更改MFMailComposeViewControllerUINavigationBar的标题颜色?

这就是我正在做的:

import MessageUI

extension MFMailComposeViewController {
    open override func viewDidLoad() {
        super.viewDidLoad()
        navigationBar.isTranslucent = false
        navigationBar.isOpaque = false
        navigationBar.barTintColor = .white
        navigationBar.tintColor = .white
        navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white]
    }
}

在 iOS 10 中工作:

在 iOS 11 中工作:

在 iOS 12 中无法正常工作:

【问题讨论】:

  • 仅供参考 - 不支持覆盖扩展中的方法,这是未定义的行为。请参阅 Swift 书籍中的扩展章节,以获得告诉您这一点的大警告。
  • 我遇到了同样的问题(但在 Objective-C 中)。我已经尝试更新initviewDidLoadviewWillAppearviewDidAppear 中的导航栏。我已经设置了窗口的tintColor,并尝试在导航栏和UINavigationBar appearance 代理上设置titleTextAttributes。 iOS 12 中的标题没有任何改变。我什至丢弃了视图层次结构,并且没有导航栏的迹象。但在 iOS 10 和 iOS 12 下都是如此。是时候向 Apple 报告错误了。
  • @ssrobbi 是的,我已经尝试为largeTitleTextAttributes 设置颜色,但没有任何改变。我认为无论如何都不会使用大标题。我确实提交了错误报告。其他人也需要这样做,让 Apple 知道它正在影响很多人。
  • 我也向 Apple 提交了一个错误。我一直无法找到一个好的解决方案或任何无法解决我问题的原因。

标签: ios iphone swift uinavigationbar mfmailcomposeviewcontroller


【解决方案1】:
/// UINavigationBar
     let navBarAppearance = UINavigationBar.appearance()
     navBarAppearance.barTintColor = .red
     navBarAppearance.tintColor = .white
     navBarAppearance.backgroundColor = .red

     navBarAppearance.titleTextAttributes = [
         .foregroundColor: UIColor.white,
         .font: UIFont.systemFontSize
     ]
     navBarAppearance.largeTitleTextAttributes = [
         .foregroundColor: UIColor.white,
         .font: UIFont.smallSystemFontSize
     ]

     let barButtonAppearance = UIBarButtonItem.appearance()
     barButtonAppearance.setTitleTextAttributes([.font: UIFont.systemFontSize], for: .normal)

     navBarAppearance.isTranslucent = false


     if #available(iOS 13.0, *) {
         let appearance = UINavigationBarAppearance()
         appearance.configureWithOpaqueBackground()
    
         appearance.backgroundColor = navBarAppearance.backgroundColor
         appearance.titleTextAttributes = navBarAppearance.titleTextAttributes ?? [:]
         appearance.largeTitleTextAttributes = navBarAppearance.largeTitleTextAttributes ?? [:]
    
         navBarAppearance.standardAppearance = appearance
         navBarAppearance.scrollEdgeAppearance = appearance
     }

【讨论】:

    【解决方案2】:

    我一直尝试改变标题颜色,但它不起作用

    在展示 mailcomopser 控制器之前

    我把背景颜色改成了白色

    按钮颜色为黑色

    下面是代码:

    UINavigationBar.appearance().setBackgroundImage(UIImage(), for: UIBarPosition.any, barMetrics: UIBarMetrics.default)
    UINavigationBar.appearance().shadowImage = UIImage()
    UINavigationBar.appearance().tintColor = UIColor.white
    UINavigationBar.appearance().barTintColor = UIColor.white
    UINavigationBar.appearance().isTranslucent = false
    UINavigationBar.appearance().clipsToBounds = false
    UINavigationBar.appearance().backgroundColor = UIColor.white
    UIBarButtonItem.appearance().setTitleTextAttributes([.foregroundColor: UIColor.black], for: .normal)
    UIBarButtonItem.appearance().setTitleTextAttributes([.foregroundColor: UIColor.black], for: .highlighted)
    UIBarButtonItem.appearance().setTitleTextAttributes([.foregroundColor: UIColor.clear], for: .disabled)
    UIBarButtonItem.appearance().setTitleTextAttributes([.foregroundColor: UIColor.black], for: .selected)
    

    【讨论】:

    • 但问题是关于如何在 iOS 12 中从 MFMailComposeViewController 更改标题颜色
    • 很遗憾,无论如何都不起作用,所以我们需要修改背景颜色和按钮颜色
    • 为什么它在 iOS 14 中不起作用?
    【解决方案3】:

    在您的 AppDelegate.swift 文件中的 didFinishLaunchingWithOptions 启动选项块中

    试试这个:

        let navigationBarAppearace = UINavigationBar.appearance()
        navigationBarAppearace.barTintColor = .blue //your desired color
        navigationBarAppearace.tintColor = .white //your button etc color 
        navigationBarAppearace.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white] //your Title Text color
    

    【讨论】:

      【解决方案4】:

      titleView 仍然可以正常工作。只需在故事板like this 中创建带有标签的视图,使用您需要的字体并将其设置为标题视图。

      if let view = Bundle.main.loadNibNamed("WTitleView", owner: self, options: nil)?.first as? UIView {
                  navigationItem.titleView = view
      } 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-10-23
        • 1970-01-01
        • 2023-03-09
        • 2019-04-14
        • 1970-01-01
        • 1970-01-01
        • 2017-02-17
        • 1970-01-01
        相关资源
        最近更新 更多