Adrian
Adrian

Reputation: 16725

Changing color of text on send/cancel buttons on MFMailComposeViewController

I'm putting the finishing touches on an app and testing on a physical device. Just as I tought I was crossing the finish line to submit to the App Store, I encountered an issue with the color a MFMailComposeViewController's send and cancel buttons. I've dug through a lot of answers here, but none of them seem to get me over the finish line.

The methods below work to send e-mail, but regardless of what I do, the color of the send/cancel buttons remains the default blue color. Any suggestions to rectify this situation are greatly appreciated.

Thank you!

    @IBAction func sendFeedbackEmail(sender: AnyObject)
    {
          feedbackButton.pop()
          print("sendFeedbackEmail called")
          if MFMailComposeViewController.canSendMail()
          {
               let mailComposeViewController = configuredMailComposeViewController()
               self.present(mailComposeViewController, animated: true, completion: nil)
          } else {
              self.showSendMailErrorAlert()
          }
     }


    
    func configuredMailComposeViewController() -> MFMailComposeViewController
    {
         let mailComposerVC = MFMailComposeViewController()
         mailComposerVC.mailComposeDelegate = self
         mailComposerVC.navigationBar.tintColor = .red
    
         mailComposerVC.navigationBar.isTranslucent = false
         mailComposerVC.navigationBar.barTintColor = .white
         mailComposerVC.setToRecipients(["[email protected]"])
         mailComposerVC.setSubject("Feedback")
    
         return mailComposerVC
    }

Upvotes: 1

Views: 4286

Answers (4)

Sachin Hunasnale
Sachin Hunasnale

Reputation: 1

Change the UIButton and bar button color before initialising MFMailCOmposeViewController and reset the color after dismissing.

let navBtnColor = UIButton.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).tintColor
let barBtnColor = UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).tintColor
let alerViewtColor = UIView.appearance().tintColor
let btnColor = UIButton.appearance().tintColor
let imageColor = UIImageView.appearance().tintColor

func reportAProblem() {
    if MFMailComposeViewController.canSendMail() {UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.systemBlue], for: .normal)
        UIButton.appearance().tintColor = UIColor.systemBlue
        UIButton.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).tintColor = UIColor.systemBlue

        let mailComposerVC = MFMailComposeViewController()
        mailComposerVC.mailComposeDelegate = self  // Extremely important to set the --mailComposeDelegate-- property, NOT the --delegate-- property
        var versionStr: String = ""

        if let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String {
            versionStr = version
        }

        mailComposerVC.setToRecipients(["[email protected]", "[email protected]"])
        mailComposerVC.setSubject("IDrive Photos: \(feedbackTextView.text ?? "")")

        let msg: String = String(format: "<html><body>\(feedbackTextView.text ?? "")<br/><br/><br/><br/><table>   <tr><td>%@</td><td>&nbsp:&nbsp</td><td>%@</td></tr>     <tr><td>%@</td><td>&nbsp:&nbsp</td><td>%@</td></tr>      <tr><td>%@</td><td>&nbsp:&nbsp</td><td>%@</td></tr>     <tr><td>%@</td><td>&nbsp:&nbsp</td><td>%@</td></tr>    </table></body></html>", "Email Address".localized, (AppConfiguration.default.username)!, "Version".localized, versionStr, "iOS Version".localized, UIDevice.current.systemVersion, "Device".localized, UIDevice.current.modelName)

        mailComposerVC.setMessageBody(msg, isHTML: true)

        // attaching file
        let documentURL = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        let destinationPath = URL.init(fileURLWithPath: documentURL + "/LogFile.txt")

        if FileManager.default.fileExists(atPath: destinationPath.path) {
            if let fileData = NSData(contentsOfFile: destinationPath.path) {
                mailComposerVC.addAttachmentData(fileData as Data, mimeType: "text/html", fileName: "LogReport")
            }
        }

        self.present(mailComposerVC, animated: true) {
            UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = UIColor.systemBlue
        }
    } else {

    }
}

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error _: Error?) {
    UIButton.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).tintColor = navBtnColor
    UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).tintColor = barBtnColor
    UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.white], for: .normal)
    UIButton.appearance().tintColor = btnColor
    UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = alerViewtColor
    UIImageView.appearance().tintColor = imageColor

    controller.dismiss(animated: true)
}

Upvotes: 0

alarsut
alarsut

Reputation: 51

In swift 4+ with the new UI style with the send button chevron, you just have to present the composerVC in modalPresentationStyle = .fullscreen. Then it will also change the color of the send button. Hope this helps.

let mailComposeVC = MFMailComposeViewController()
mailComposeVC.mailComposeDelegate = self
mailComposeVC.modalPresentationStyle = .fullScreen
mailComposeVC.navigationBar.tintColor = UIColor(red:131/255.0, green:184/255.0, blue:26/255.0, alpha:1)
self.present(mailComposeVC, animated: true, completion: nil)

I searched for hours and found nothing. I just tried it randomly and got the solution. I hope this helps anyone who does search for this problem.

enter image description here

Upvotes: 1

Durul Dalkanat
Durul Dalkanat

Reputation: 7425

Here is the answer. I hope, you are not using baseViewController for UIViewController.

            @IBAction func sendFeedbackEmail(sender: AnyObject) {
             feedbackButton.pop()
             print("sendFeedbackEmail called")
        
             if MFMailComposeViewController.canSendMail() 
             {
               let mailComposeViewController = configuredMailComposeViewController()
               mailComposeViewController.navigationBar.tintColor = UIColor.redColor()
              self.presentViewController(mailComposeViewController, animated: true, completion: nil)
              
             } else {
                 self.showSendMailErrorAlert()
             }
         }

Upvotes: 4

Abhishek Jain
Abhishek Jain

Reputation: 4739

Swift 4.0

mailComposeViewController.navigationBar.tintColor = UIColor.red

Upvotes: 10

Related Questions