My problem is with the navigationController
the rules just don't apply.
override func viewDidLoad() {
super.viewDidLoad()
navigationBarColor()
}
func navigationBarColor() {
navigationController?.navigationBar.barTintColor = UIColor(red:0.91, green:0.04, blue:0.51, alpha:1.0)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
}
Here's the result - The color is not there
What should I do?
Your code setting the style properties looks correct. It's likely that navigationController
is nil.
A better approach is to use a custom navigation controller subclass and connect it to the navigation controller in Interface builder.
open class NavigationController: UINavigationController {
open override func viewDidLoad() {
super.viewDidLoad()
navigationBar.barTintColor = UIColor.blue
}
}
first you set bartintColor and after that you set the "clear image" in navigation bar :) of course it is be transparent.
you need image with some color or create it in code: (for example)
extension UIImage {
class func imageWithColor(_ color: UIColor) -> UIImage {
let rect = CGRect(origin: CGPoint.zero, size: CGSize(width: 1.0, height: 1.0))
UIGraphicsBeginImageContext(rect.size);
guard let context = UIGraphicsGetCurrentContext() else {
UIGraphicsEndImageContext()
return UIImage()
}
context.setFillColor(color.cgColor);
context.fill(rect);
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image ?? UIImage()
}
}
and set image in navigation bar like this:
call this navigationBar changes when controller will appearing
func updatenavigationBar() {
navigationController?.navigationBar.tintColor = .white
navigationController?.navigationBar.isTranslucent = true
navigationController?.navigationBar.setBackgroundImage(UIImage.imageWithColor(color), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
}
I'm not quite sure what you're asking based on the question but if you are trying to change the color of the navigation bar I would suggest changing :
navigationController?.navigationBar.barTintColor = UIColor(red:0.91, green:0.04, blue:0.51, alpha:1.0)
To:
navigationController?.navigationBar.backgroundColor = UIColor(red:0.91, green:0.04, blue:0.51, alpha:1.0)
This will add the color as the background color instead of just the tint color.
Use this code
import Foundation
import Swift
extension UINavigationController
{
func setMainTopNavigationBarAttribute() -> Void
{
self.navigationBar.shadowImage = UIImage()
self.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationBar.isTranslucent = false
self.navigationBar.tintColor = UIColor.white
self.navigationBar.backgroundColor = UIColor.white
}
}
You can use this way
let navigationController = UIStoryboard.user.instantiateViewController(withIdentifier: "MMNavigationController") as! UINavigationController
navigationController.setMainTopNavigationBarAttribute()
You just change this two line in your function
func navigationBarColor() {
navigationController?.navigationBar.barTintColor = UIColor(red:0.91, green:0.04, blue:0.51, alpha:1.0)
//change this two line in your function
navigationController?.navigationBar.shadowImage = UIImage.init(named: "")
navigationController?.navigationBar.setBackgroundImage(UIImage.init(named: ""), for: .default)
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.