简体   繁体   English

在MFMessageComposeViewController上设置NavigationBar背景色

[英]Set NavigationBar background color on MFMessageComposeViewController

I have a problem changing background color of navigation bar on MFMessageComposeViewController . 我在更改MFMessageComposeViewController上导航栏的背景颜色时MFMessageComposeViewController

I've tried this code: 我已经试过这段代码:

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
}

And this is not working. 这是行不通的。 The most strange thing is that it do work when I do the same for MFMailComposeViewController . 最奇怪的是,当我对MFMailComposeViewController执行相同操作时,它确实起作用。

I also tried to change color directly on composer like this. 我还试图像这样直接在作曲家上更改颜色。

composer.navigationBar.tintColor = Configuration.Colors.navigationBarBackgroundColor

I looks that i found workaround. 我看起来找到了解决方法。 Somehow setting composer.navigationBar.barTintColor and UINavigationBar.appearance().barTintColor are not working. 以某种方式设置composer.navigationBar.barTintColorUINavigationBar.appearance().barTintColor无效。

The workaround is to use UINavigationBar.appearance().setBackgroundImage(...) and set UIImage with one color as background 解决方法是使用UINavigationBar.appearance().setBackgroundImage(...)并将一种颜色的UIImage设置为背景

Full working code: 完整的工作代码:

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

to create UIImage with one color: 用一种颜色创建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!
    }
}

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

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