Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a line below Navigation bar in the pushed View Controller?

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.

like image 213
SriTeja Chilakamarri Avatar asked Nov 29 '18 04:11

SriTeja Chilakamarri


People also ask

How do I customize my navigation bar?

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.


2 Answers

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

enter image description here

like image 199
McDonal_11 Avatar answered Nov 14 '22 20:11

McDonal_11


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()
}
like image 33
Natarajan Avatar answered Nov 14 '22 21:11

Natarajan