Change title color of navigation bar in MFMailComp

2020-02-17 10:15发布

问题:

How can I change the title color of UINavigationBar in MFMailComposeViewController in iOS 12?

This is what I am doing:

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

In iOS 10 works:

In iOS 11 works:

In iOS 12 is not working:

回答1:

I tried all the way to change the title color, however it doesn't work

Before presenting the mailcomopser controller

I changed the background color to white

and buttons color to black

Here is the code below:

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)



回答2:

In your AppDelegate.swift file in the didFinishLaunchingWithOptions launchOptions block

TRY THIS:

    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


回答3:

titleView still works fine. Just create view with label in story board like this, with such font, as U need and set it as title view.

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