Height of Navigation Bar in swift 4

Viewed 2517

In swift 3, I was able to change the height of a custom navigation bar using constraints and simply setting the height. Now I cannot do that anymore. It doesn't actually change the height of the navigation bar in the storyboard. Even when I go to the size inspector, the height field is gray and I am not able to change it. What is the best practice if I am presenting a view modally and want to display a navigation bar? It doesn't display automatically since it is a modally segue.

1 Answers

I had a similar issue, so i started to research and i found a solution that works for me... I'm gonna show how i did it, but i have to say that i don't use storyboards, so it is all in code...

1) You have to create a class of type "UIView", and then put this code in it:

class ExtendedNavBarView: UIView {

override func willMove(toWindow newWindow: UIWindow?) {
    super.willMove(toWindow: newWindow)

    layer.shadowOffset = CGSize(width: 0, height: CGFloat(1) / UIScreen.main.scale)
    layer.shadowRadius = 0

    layer.shadowColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1).cgColor
    layer.shadowOpacity = 0.25
}

2) Then, go to the class where you have your navigationController. In the viewDidLoad method, put this code:

    navBar.shadowImage = #imageLiteral(resourceName: "TransparentPixel")

    let extendedBar = ExtendedNavBarView()
    extendedBar.frame = CGRect(x: 0, y: 0, width: view.bounds.width, height: 82) //The height value is gonna determinate the total height of the navbar 
    view.addSubview(extendedBar)

    extendedBar.backgroundColor = UIColor(red: 249/255, green: 249/255, blue: 249/255, alpha: 1.0)

Basically is an extension view for the navigationBar, depending of the value you put in the height, the total navbar is gonna be more or less taller.

If you want to learn more about it, you can go here: Custom NavigationBar

Also, the "TransparentPixel" image is in the page, you just have to download the sample project.

Related