在MFMessageComposeViewController上设置NavigationBar背景颜色

jbl*_*der 6 navigationbar ios swift mfmailcomposeviewcontroller

我在更改导航栏的背景颜色时遇到问题MFMessageComposeViewController.

我试过这段代码:

UINavigationBar.appearance().barTintColor = Configuration.Colors.navigationBarBackgroundColor
UINavigationBar.appearance().backgroundColor = UIColor.green
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: UIFont(name: "Roboto-Regular", size: 18)!, NSForegroundColorAttributeName: UIColor.white] as [String: AnyObject]

let composer = MFMessageComposeViewController() 

self?.present(composer, animated: true) {
    UIApplication.shared.statusBarStyle = .lightContent
}
Run Code Online (Sandbox Code Playgroud)

这不起作用.最奇怪的是,当我做同样的事情时,它确实有效MFMailComposeViewController.

我也尝试直接在作曲家上改变颜色.

composer.navigationBar.tintColor = Configuration.Colors.navigationBarBackgroundColor
Run Code Online (Sandbox Code Playgroud)

jbl*_*der 3

我看起来我找到了解决方法。不知何故设置composer.navigationBar.barTintColorUINavigationBar.appearance().barTintColor不起作用。

解决方法是使用 UINavigationBar.appearance().setBackgroundImage(...)UIImage 并设置一种颜色作为背景

完整的工作代码:

UINavigationBar.appearance().setBackgroundImage(UIImage.from(color: UIColor.green), for: .default)
let composer = MFMessageComposeViewController()       
self?.present(composer, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

UIImage使用一种颜色创建:

extension UIImage {
    static func from(color: UIColor) -> UIImage {
        let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
        UIGraphicsBeginImageContext(rect.size)
        let context = UIGraphicsGetCurrentContext()
        context!.setFillColor(color.cgColor)
        context!.fill(rect)
        let img = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return img!
    }
}
Run Code Online (Sandbox Code Playgroud)