5

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
Cœur
  • 37,241
  • 25
  • 195
  • 267
jblejder
  • 1,373
  • 1
  • 10
  • 11
  • 1
    Please check this link [Link to set navigation on mail composer ](https://stackoverflow.com/questions/39453933/ios-10-can-no-longer-set-barcolor-and-tint-on-mfmessagecomposeviewcontroller) – cole Aug 21 '17 at 11:24
  • Change in info.plist the row View controller-based status bar appearance and set it to NO – Iulian David Aug 21 '17 at 11:54
  • The property for setting the background colour is `composer.navigationBar.barTintColor` try it it will work. – Nishant Bhindi Aug 22 '17 at 05:54

1 Answers1

3

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!
    }
}
jblejder
  • 1,373
  • 1
  • 10
  • 11