【问题标题】:How to change the Navigation Bar color for a MFMessageComposeViewController?如何更改 MFMessageComposeViewController 的导航栏颜色?
【发布时间】:2017-02-10 19:02:26
【问题描述】:

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

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

但是 AppDelegate 没有设置 Message VC 的样式,但我不确定为什么不这样做。 我试过了,但没有任何改变。 让控制器 = MFMessageComposeViewController()

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

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

这是电子邮件 VC 和消息 VC 导航栏的照片。

如您所见,Message VC 的样式不像 Email VC 导航栏,但我不确定为什么。

【问题讨论】:

标签: ios xcode swift3 uinavigationbar mfmessagecomposeview


【解决方案1】:

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

那么,MFMessageComposeViewController继承自UINavigationController,就可以使用它的初始化方法了

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

并提供MyNavigationBar 类作为参数。

【讨论】:

  • 为什么 Mail VC 被 AppDelegate 设置样式而不是 Mail VC?
  • 请澄清您的问题。我没听懂。
  • 为什么 MFMailComposeViewController 的导航栏接受 AppDelegate 文件中设置的样式,而 MFMessageComposeViewController 不接受相同的样式。
  • 它可以使用自定义导航栏覆盖原始导航栏,也可以覆盖其实现导航栏的属性。貌似是iOS系统问题。您可以提交有关此的错误报告。例如,与原始操作表相同的行为。它使用 UIButton 的系统子类作为其按钮,但它们没有获得 UIButton 的外观属性。
【解决方案2】:

以下内容适用于 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 ...
    }
}

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

问候。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-06
    • 1970-01-01
    • 2021-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多