I have a UINavigation Controller which is used to push or pop Views. In the Initial view controller I want to hide the navigation Bar bottom 1 Pixel shadow. So I here's the code for that.
func setup(){
if #available(iOS 11.0, *) {
self.navigationController?.navigationBar.prefersLargeTitles = true
self.navigationController?.navigationItem.largeTitleDisplayMode = .always
} else {
// Fallback on earlier versions
}
self.navigationBar.isTranslucent = true
self.navigationBar.clipsToBounds = true
self.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationBar.shadowImage = UIImage()
self.navigationBar.tintColor = UIColor(hexString: "#373839")
self.navigationBar.backgroundColor = UIColor.white
}
But when I push to second View controller the Navigation Bar's shadow is hidden even in this.
Does setting the Navigation Bar's properties in the Parent view controller effect those in all the controllers pushed from there on ? I thought Navigation Bar is specific to View controller a Navigation controller creates a new Navigation Bar for each pushed view.
Could someone help me understand this and how I could have 1 pixel shadow back on the Navigation Bar for only 1 view.
From Settings, tap Display, and then tap Navigation bar. Make sure Buttons is selected, and then you can choose your desired button setup at the bottom of the screen. Note: This option will also affect the location you swipe when using Swipe gestures.
Add below code in ViewController
where you want to change color of NavigationBar's Shadow
.
func addColorToShadow() {
self.navigationController?.navigationBar.clipsToBounds = false
self.navigationController?.navigationBar.shadowImage = UIColor(red: 215/255, green: 215/255, blue: 215/255, alpha: 1.0).image(CGSize(width: self.view.frame.width, height: 1))
}
extension UIColor {
func image(_ size: CGSize = CGSize(width: 1, height: 1)) -> UIImage {
return UIGraphicsImageRenderer(size: size).image { rendererContext in
self.setFill()
rendererContext.fill(CGRect(x: 0, y: 0, width: size.width, height: size.height))
}
}
}
Output
I think. When you change the properties of Navigation bar in Navigation controller, It will be applied to all view controller's Nav Bar. So you may have to reset your Nav Bar's property. have you tried as like below?
For example:
//In viewWillDisappear
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationBar.setBackgroundImage(nil, for: .default)
self.navigationBar.shadowImage = nil
}
//In viewWillAppear
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationBar.shadowImage = UIImage()
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With