swiftemailmessageui

MFMailComposeViewController not showing Cancel button


I tried all the SOF solutions nothing work for my case.

My app is using tabbar and one of the tabbar is setting(tableview). User can tap on the support cell to send email.

I can bring up the email view but it doesn't have cancel button. Only way to dismiss it is to send the email or swipe down save/delete draft.

Thanks!!

screenshot

import UIKit
import MessageUI

class SettingVC: UIViewController, UITableViewDelegate, UITableViewDataSource, MFMailComposeViewControllerDelegate {
    @IBOutlet weak var tableView: UITableView!

    let mail = MFMailComposeViewController()
    var fromSetting: Bool = false
    let settingsSction = ["Section0", "Section1", "Section2"]
    let settingsCell = [["Cell0"],
                        ["Cell1"],
                        ["Cell2", "Support"]]

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.navigationController?.navigationBar.barStyle = .black
    }

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        setUpNavBar()

        tableView.delegate = self
        tableView.dataSource = self
        mail.mailComposeDelegate = self
    }

    func setUpNavBar() {
        self.navigationItem.titleView?.tintColor = .white
        let settingsTitleLabel = UILabel()
        settingsTitleLabel.textColor = .white
        settingsTitleLabel.font = UIFont.boldSystemFont(ofSize: 18)
        settingsTitleLabel.text = "Settings"
        self.navigationItem.titleView = settingsTitleLabel
    }

    //MARK: Table View Sections
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 50
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return settingsSction.count
    }

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return settingsSction[section]
    }

    //MARK: Table View Cell Title
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return settingsCell[section].count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = "\(settingsCell[indexPath.section][indexPath.row])"
        cell.selectionStyle = .none

        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        switch indexPath.section {
        case 2:
            switch indexPath.row {
            case 1:
                sendEmail()
            default:
                break
            }
        default:
            break
        }
    }

    func sendEmail() {
        if MFMailComposeViewController.canSendMail() {
            mail.navigationBar.tintColor = UIColor.orange
            mail.setToRecipients(["support@example.com"])
            mail.setSubject("I have an issue.")

            self.present(mail, animated: true, completion: nil)
        } else {
           alertOK(title: "No Mail App Available", message: "Please install Mail app in your phone or use other mail app to send us the issue. Thank you.", sender: self)
        }
    }

    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
        controller.dismiss(animated: true, completion: nil)
    }
}

Solution

  • Issue solved. I was setting the navigation bar color in app delegate so it makes the text white color as well.