更改iOS 12中MFMailComposeViewController中导航栏的标题颜色不起作用

时间:2018-09-26 16:26:19

标签: ios iphone swift uinavigationbar mfmailcomposeviewcontroller

如何更改UINavigationBarMFMailComposeViewControlleriOS 12的标题颜色?

这就是我在做什么:

import MessageUI

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

在iOS 10中工作:

ios 10

在iOS 11中工作:

ios 11

在iOS 12中不起作用:

ios 12

5 个答案:

答案 0 :(得分: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([NSAttributedStringKey.foregroundColor: UIColor.black], for: .normal)
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.black], for: .highlighted)
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: .disabled)
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.black], for: .selected)

enter image description here

答案 1 :(得分:0)

我希望这对所有人都有帮助,我遇到了这个问题,被困了大约2个小时,但是我知道了解决方案。您不需要重写rmaddy提到的功能,它可以产生意外的结果。 解决方案是在实例化MFMailComposeViewController之前使用UINavigation外观,并且该属性将按预期工作。 林添加示例代码,我如何使用它。希望对您有帮助:)

代码

  func setNavigatinAppearanceToBlack() {

    UINavigationBar.appearance().titleTextAttributes =
        [NSFontAttributeName: AppFont.appFont(fontType: .bold, size: 18)]
    let attributes:Dictionary = [NSFontAttributeName :
        AppFont.appFont(fontType: .bold, size: 18), NSForegroundColorAttributeName : UIColor.black]
    UIBarButtonItem.appearance().setTitleTextAttributes(attributes as [String : Any]?, for: .normal)

}
func sendEmailToPrimaryEmail(){

    if (MFMailComposeViewController.canSendMail()){

        setNavigatinAppearanceToBlack() // here change the property as you need
        let mailComposerVC = MFMailComposeViewController()
        mailComposerVC.mailComposeDelegate = self
        mailComposerVC.setToRecipients([(self.contactDetail?.response?.data?.contactDetail?.contactEmail1)!])
        mailComposerVC.view.tintColor = UIColor.black
        mailComposerVC.setMessageBody("", isHTML: false)
        self.present(mailComposerVC, animated: true, completion: nil)
    }else{
        self.showAlert(message: "Looks like, your email is not configured!")
    }
}

答案 2 :(得分:0)

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

答案 3 :(得分:-2)

titleView仍然可以正常工作。只需在故事板like this中使用标签(如U所需)创建带有标签的视图,并将其设置为标题视图即可。

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

like this

答案 4 :(得分:-3)

在您的AppDelegate.swift文件中didFinishLaunchingWithOptions launchOptions块

请尝试:

    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