Change title color of navigation bar in MFMailComposeViewController in iOS 12 not working [duplicate]
Asked Answered
I

4

36

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 = [.foregroundColor: UIColor.white]
    }
}

In iOS 10 works:

ios 10

In iOS 11 works:

ios 11

In iOS 12 is not working:

ios 12

Intransigent answered 26/9, 2018 at 16:26 Comment(12)
You are not alone: #52451656Margretmargreta
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.Margretmargreta
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.Margretmargreta
@Margretmargreta Are you using large titles? Make sure you have set the largeTitleTextAttributes, not just the titleTextAttributes. edit: and same for UIAppearanceBlanding
not working. Just a bug. Even large title. Seems they skip the appearance loadingCyprinoid
@Margretmargreta 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 MFMailComposeViewControllerIntransigent
@Intransigent 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.Margretmargreta
@Blanding 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.Margretmargreta
@Margretmargreta 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.Intransigent
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.Horton
Anyone wants to post the radar? @GabrielPires maybe?Horseleech
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 = .whiteStated
E
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
     }
Eisenach answered 5/7, 2021 at 8:53 Comment(0)
C
-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([.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)

enter image description here

Centurion answered 4/4, 2019 at 7:55 Comment(3)
But the question is about how to change the title color from MFMailComposeViewController in iOS 12Intransigent
Unfortunately, it doesn't work in any case, so we need to modify the background color and button colorsCenturion
Why it doesn't work in iOS 14?Congruous
I
-6

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
Ierna answered 27/10, 2018 at 0:4 Comment(0)
R
-6

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

Remake answered 7/4, 2019 at 15:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.