Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set NavigationBar background color on MFMessageComposeViewController

I have a problem changing background color of navigation bar on 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.

I also tried to change color directly on composer like this.

composer.navigationBar.tintColor = Configuration.Colors.navigationBarBackgroundColor
like image 752
jblejder Avatar asked Oct 18 '22 07:10

jblejder


1 Answers

I looks that i found workaround. Somehow setting composer.navigationBar.barTintColor and UINavigationBar.appearance().barTintColor are not working.

The workaround is to use UINavigationBar.appearance().setBackgroundImage(...) and set UIImage with one color as background

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:

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!
    }
}
like image 141
jblejder Avatar answered Nov 15 '22 06:11

jblejder