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

时间:2017-02-10 19:02:26

标签: ios xcode swift3 uinavigationbar mfmessagecomposeview

我正在使用MFMessageComposeViewController和MFMailComposeViewController。出于某种原因,只有Mail VC使用我想要的颜色进行样式设置。以下是我在didFinish func中的AppDelegate中设置导航栏的样式。

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

但是消息VC没有被AppDelegate设计,但我不确定为什么不这样做。 我尝试了这个,但没有改变。     让controller = MFMessageComposeViewController()

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

Message VC的样式是否有所不同?它仍然显示默认的白色导航栏和默认的蓝色取消按钮。

以下是Email VC和Message VC导航栏的照片。 enter image description here enter image description here

正如您所看到的,消息VC的样式不像电子邮件VC导航栏,但我不确定原因。

2 个答案:

答案 0 :(得分:1)

您可以创建UINavigationBarMyNavigationBar)的子类,在其中设置所有需要的属性。

然后,当MFMessageComposeViewController继承自UINavigationController时,您可以使用其初始化方法

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

并提供MyNavigationBar类作为参数。

答案 1 :(得分:0)

以下是Swift 3/4。

我在StackOverflow和其他网站上尝试了很多方法,包括上面答案中提到的子类方式。但无法在改变颜色或改变UIBarButtons的字体颜色方面取得成功。

然后尝试了呈现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 ...
    }
}

希望,这对像我这样的人有用。

问候。