Search code examples
iosswiftmfmailcomposeviewcontroller

How to customize MFMailComposeViewController in iOS 14


I want to customize MFMailComposeViewController style and its behaviour in my app, but all my attempts fail. I tried several ways: First way:

let mailComposeViewController = MFMailComposeViewController()
...
UINavigationBar.appearance().tintColor = UIColor.red //don't see changes
UINavigationBar.appearance().barTintColor = UIColor.green //don't see changes
UINavigationBar.appearance().backgroundColor = UIColor.yellow //don't see changes
UINavigationBar.appearance().isTranslucent = false //don't see changes
UINavigationBar.appearance().clipsToBounds = false //don't see changes
UINavigationBar.appearance().prefersLargeTitles = false //don't see changes

present(mailComposeViewController, animated: true, completion: nil)

Second way:

extension MFMailComposeViewController {
    open override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationBar.clipsToBounds = true //don't see changes
        self.navigationBar.prefersLargeTitles = false //don't see changes
        self.navigationBar.isTranslucent = false //don't see changes
        self.navigationBar.barTintColor = UIColor.green //don't see changes
        self.navigationBar.tintColor = UIColor.red //only this works!
    }
}

PS What I'm most interested in is changing the prefersLargeTitles to false or change navigationBarStyle like here :

exemple

My result:

exemple2


Solution

  • From Apple:

    Important You must not modify the view hierarchy presented by this view controller. However, you can customize the appearance of the interface using the UIAppearance protocol.

    I think Apple does not approve of your code on the Second way, you're not supposed to modify this view. However, Try:

    UINavigationBar.appearance().tintColor = UIColor.red 
    UINavigationBar.appearance().barTintColor = UIColor.green 
    UINavigationBar.appearance().backgroundColor = UIColor.yellow 
    UINavigationBar.appearance().isTranslucent = false 
    UINavigationBar.appearance().clipsToBounds = false 
    UINavigationBar.appearance().prefersLargeTitles = false 
    
    let mailComposeViewController = MFMailComposeViewController()
    present(mailComposeViewController, animated: true, completion: nil)