简体   繁体   中英

How to change the Navigation Bar color for a MFMessageComposeViewController?

I'm using the MFMessageComposeViewController and the MFMailComposeViewController. For some reason only the Mail VC is being styled with the colors I want. Here is how I am styling the Navigation bar in the AppDelegate inside the didFinish func.

let navigationBarAppearace = UINavigationBar.appearance()
    navigationBarAppearace.tintColor = Styles.whiteColor()
    navigationBarAppearace.barTintColor = Styles.inputColor()
    navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName:Styles.whiteColor()]
    navigationBarAppearace.isTranslucent = false

But the Message VC is not being styled by the AppDelegate but I'm not sure why not. I tried this but nothing changed. let controller = MFMessageComposeViewController()

        controller.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: Styles.positiveColor()]
        controller.navigationBar.barTintColor = Styles.negativeColor()
        controller.messageComposeDelegate = self

Is the Message VC styled differently? It still shows up with the default white nav bar and the default blue cancel button.

Here is a photo of the Email VC and the Message VC navigations bars. 在此处输入图片说明 在此处输入图片说明

As you can see the Message VC is not being styled like the Email VC Navigation bar, but I'm not sure why.

You can create a subclass of UINavigationBar ( MyNavigationBar ) where you set all needed properties.

Then, as MFMessageComposeViewController inherits from UINavigationController , you can use its initialization method

init(navigationBarClass: AnyClass?, toolbarClass: AnyClass?)

and provide MyNavigationBar class as a parameter.

The following is for Swift 3/4.

I tried many ways shown on StackOverflow and other sites, including the subclass way mentioned in the above answer. But could not get success in changing color or changing font color of UIBarButtons.

Then tried different way of presenting the MFMessageComposeViewController.

// Configures and returns a MFMessageComposeViewController instance. This is same with no change.
func configuredMessageComposeViewController() -> MFMessageComposeViewController {
    let messageComposeVC = MFMessageComposeViewController()

    let fileManager:FileManager = FileManager.default
    messageComposeVC.messageComposeDelegate = self  //  Make sure to set this property to self, so that the controller can be dismissed!
    messageComposeVC.recipients = [myContactPhone]

    if fileManager.fileExists(atPath: mySendImagePath) {
        if let image = UIImage(contentsOfFile: mySendImagePath) {
            if UIImagePNGRepresentation(image) != nil
            {
                let imageData1: Data = UIImagePNGRepresentation(image)!
                let success = messageComposeVC.addAttachmentData(imageData1, typeIdentifier: "public.data", filename: "image.JPG")

                if(success)
                {
                }
                else{
                }
            }
        }
    }
    return messageComposeVC
}

// Following code is usage of above.
    if (MFMessageComposeViewController.canSendText()) {
        myMessageComposeVC = configuredMessageComposeViewController()

        // old code - Instead of using following way
        //present(messageComposeVC, animated: true, completion: nil)

        // Used this way to use existing navigation bar.
        if let messageComposeVC = myMessageComposeVC {
            messageComposeVC.willMove(toParentViewController: self)
            messageComposeVC.view.frame = self.view.frame
            self.view.addSubview(messageComposeVC.view)
            self.addChildViewController(messageComposeVC)
            messageComposeVC.didMove(toParentViewController: self)
        }
    } else {
        showSendMMSErrorAlert()
        return
    }

// Following code to remove it when returned through delegate.
func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {

    // old code
    //controller.dismiss(animated: true, completion: nil)

    controller.willMove(toParentViewController: nil)
    controller.view.removeFromSuperview()
    controller.removeFromParentViewController()

    if(result.rawValue == 0)
    {
        ... error ...
    } else {
        ... success ...
    }
}

Hope, this is useful for persons like me.

Regards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM