简体   繁体   English

如何更改MFMessageComposeViewController的导航栏颜色?

[英]How to change the Navigation Bar color for a MFMessageComposeViewController?

I'm using the MFMessageComposeViewController and the MFMailComposeViewController. 我正在使用MFMessageComposeViewController和MFMailComposeViewController。 For some reason only the Mail VC is being styled with the colors I want. 由于某些原因,只有Mail VC才使用我想要的颜色进行样式设置。 Here is how I am styling the Navigation bar in the AppDelegate inside the didFinish func. 这是我在didFinish功能内的AppDelegate中设置导航栏样式的方式。

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. 但是Message VC不是由AppDelegate设置样式的,但我不确定为什么不这样做。 I tried this but nothing changed. 我尝试了这个,但没有改变。 let controller = MFMessageComposeViewController() 让控制器= MFMessageComposeViewController()

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

Is the Message VC styled differently? Message VC的样式是否不同? 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. 这是Email VC和Message VC导航栏的照片。 在此处输入图片说明 在此处输入图片说明

As you can see the Message VC is not being styled like the Email VC Navigation bar, but I'm not sure why. 如您所见,Message VC的样式不像Email VC导航栏,但我不确定为什么。

You can create a subclass of UINavigationBar ( MyNavigationBar ) where you set all needed properties. 您可以创建UINavigationBar的子类( MyNavigationBar ),在其中设置所有需要的属性。

Then, as MFMessageComposeViewController inherits from UINavigationController , you can use its initialization method 然后,由于MFMessageComposeViewController继承自UINavigationController ,因此可以使用其初始化方法

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

and provide MyNavigationBar class as a parameter. 并提供MyNavigationBar类作为参数。

The following is for Swift 3/4. 以下是Swift 3/4的内容。

I tried many ways shown on StackOverflow and other sites, including the subclass way mentioned in the above answer. 我尝试了StackOverflow和其他站点上显示的许多方法,包括上面答案中提到的子类方法。 But could not get success in changing color or changing font color of UIBarButtons. 但是无法成功更改UIBarButtons的颜色或字体颜色。

Then tried different way of presenting the MFMessageComposeViewController. 然后尝试以其他方式呈现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. 问候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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