Cancel button text color on Mail Compose View Controller

318 views Asked by At

I am using iOS 14 device and whenever I open Mail View Composer, the 'Cancel' button on top left corner always shows up in white. I was able to set the color of send button, but not able to change the color for 'Cancel' button.

    let mailComposerVC = MFMailComposeViewController()
    mailComposerVC.mailComposeDelegate = self
    mailComposerVC.setToRecipients(toRecipientEmailIds)
    mailComposerVC.setSubject(subject)
    mailComposerVC.setMessageBody(messageBody, isHTML: false)

    if let topVC = UIApplication.shared.topMostViewController() {
        mailComposerVC.navigationBar.tintColor = UIColor(rgb: 0x057AFF)
        //mailComposerVC.navigationBar.isTranslucent = false
        //mailComposerVC.navigationBar.isOpaque = false
        UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).tintColor = UIColor.white
        topVC.present(mailComposerVC, animated: true)
    }

How do I change text color for 'Cancel' button?

Thanks!

2

There are 2 answers

3
Shawn Frank On

Remove the below as it probably is what is causing your cancel button to be white

UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).tintColor = UIColor.white

You only need this

// set any color you want
mailComposerVC.navigationBar.tintColor = .green

MFMailComposerView cancel button navigation bar button tint swift iOS

0
timyau On

Maybe your AppDelegate.swift has set the value for all the NavigateBar

UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.white], for: .normal)

Just remove above line, you will be able to set your mail cancel button color.

Please refer to this post: Cancel and send button missing on MFMailComposeViewController iOS