34

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:

ios 10

In iOS 11 works:

ios 11

In iOS 12 is not working:

ios 12

pableiros
  • 10,752
  • 11
  • 79
  • 78
  • 1
    You are not alone: https://stackoverflow.com/questions/52451156/ios-12-0-is-there-a-way-to-set-mfmailcomposeviewcontroller-navigation-bar-titl – rmaddy Sep 26 '18 at 16:29
  • 3
    FYI - Overriding a method in an extension is not supported and it is undefined behavior. Please see the Extensions chapter in the Swift book for a large warning telling you this. – rmaddy Sep 27 '18 at 00:17
  • I'm having the same issue (but in Objective-C). I've tried updating the navigation bar in `init`, `viewDidLoad`, `viewWillAppear`, and `viewDidAppear`. I've set the window's `tintColor` and I've tried setting the `titleTextAttributes` on both the nav bar and the `UINavigationBar appearance` proxy. Nothing has changed the title in iOS 12. I've even dumped the view hierarchy and there's no sign of a nav bar. But that's true under iOS 10 as well as iOS 12. Time for a bug report to Apple. – rmaddy Sep 27 '18 at 00:22
  • @rmaddy Are you using large titles? Make sure you have set the `largeTitleTextAttributes`, not just the `titleTextAttributes`. edit: and same for UIAppearance – ssrobbi Sep 27 '18 at 01:26
  • not working. Just a bug. Even large title. Seems they skip the appearance loading – E.Coms Sep 27 '18 at 01:27
  • @rmaddy is ok. I'm overriding a method from extension because the New Message view controller is opening from a `UIActivityViewController` and I'm not instantiating a `MFMailComposeViewController` – pableiros Sep 27 '18 at 02:15
  • @pableiros It's not really OK. It may work. But it may not work. It's undefined behavior to override a method in an extension. It's best to avoid attempting it. – rmaddy Sep 27 '18 at 02:36
  • 1
    @ssrobbi Yeah, I already tried setting the color for the `largeTitleTextAttributes` but nothing changed. I don't think large titles are being used anyway. I did file a bug report. Others need to do the same to let Apple know it's affecting lots of people. – rmaddy Sep 27 '18 at 02:37
  • @rmaddy sorry for my english, what I tried to say is I'm agree with your argument about to not override methods from extensions. I did that way because I didn't find another way to achieve that. – pableiros Sep 27 '18 at 02:54
  • 1
    I have filed a bug with Apple as well. I have been unable to find a nice solution or any reason why it wouldn't be working on my end. – Gabriel Pires Oct 17 '18 at 23:14
  • Anyone wants to post the radar? @GabrielPires maybe? – Claus Jørgensen Jul 09 '19 at 14:41
  • has anyone found an acceptable way of doing this? it is still an issue in ios13! I have tried every solution posted and i could think of. also tried setting this in the appdelegate where there rest of my apps navigation bar UI is set, didnt work `UINavigationBar.appearance(whenContainedInInstancesOf: [MFMailComposeViewController.self]).tintColor = .white` – Tahdiul Haq Nov 20 '19 at 21:18

3 Answers3

0

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)

enter image description here

Fast Coderz
  • 304
  • 1
  • 11
-4

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

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
} 

like this

Bhargav Rao
  • 41,091
  • 27
  • 112
  • 129