簡體   English   中英

如何更改MFMessageComposeViewController的導航欄顏色?

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

我正在使用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

但是Message VC不是由AppDelegate設置樣式的,但我不確定為什么不這樣做。 我嘗試了這個,但沒有改變。 讓控制器= MFMessageComposeViewController()

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

Message VC的樣式是否不同? 它仍然顯示帶有默認的白色導航欄和默認的藍色取消按鈕。

這是Email VC和Message VC導航欄的照片。 在此處輸入圖片說明 在此處輸入圖片說明

如您所見,Message VC的樣式不像Email VC導航欄,但我不確定為什么。

您可以創建UINavigationBar的子類( MyNavigationBar ),在其中設置所有需要的屬性。

然后,由於MFMessageComposeViewController繼承自UINavigationController ,因此可以使用其初始化方法

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

並提供MyNavigationBar類作為參數。

以下是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 ...
    }
}

希望這對像我這樣的人有用。

問候。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM