Set the backgroud NavigationBar color to MFMessageComposeViewController

I have a problem with changing the background color of the navigation bar on the MFMessageComposeViewController .

I 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 it does not work. The strangest thing is that it works when I do the same for MFMailComposeViewController .

I also tried changing the color directly on a composer like this.

 composer.navigationBar.tintColor = Configuration.Colors.navigationBarBackgroundColor 

I can not find a solution compared to stackoverflow.

+5
source share
1 answer

I look that I have found a workaround. Somehow setting composer.navigationBar.barTintColor and UINavigationBar.appearance().barTintColor does not work.

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

Full working code:

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

create UIImage 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! } } 
+1
source

Source: https://habr.com/ru/post/1271052/


All Articles