Chandler Long
Chandler Long

Reputation: 77

Swift 4: Unable to programmatically change the height on my Navigation Bar

I have a custom Navigation Bar that I am setting up for my ViewController. The problem is that the Navigation Bar is too small and I am unable to change the height of it.

class AddGameViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = Color.lightGrey
    self.setNavigationBar()

}
func setNavigationBar() {
    let screenSize: CGRect = UIScreen.main.bounds
    let navBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: screenSize.width, height: 50))
    let navItem = UINavigationItem(title: "Create Game")
    let doneItem = UIBarButtonItem(image: UIImage(named: "Cancel"), style: .plain, target: self, action: #selector(cancelGameCreation))
    navItem.leftBarButtonItem = doneItem
    navBar.setItems([navItem], animated: false)
    self.view.addSubview(navBar)
}

@objc func cancelGameCreation() {
    dismiss(animated: true, completion: nil)
    }
}

No matter what my height is set to. This is the appearance of my Navigation bar

Upvotes: 0

Views: 1038

Answers (2)

Steve
Steve

Reputation: 941

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.lightGray
        self.setNavigationBar()  
    }

    func setNavigationBar() {
        let navBar = UINavigationBar()
        navBar.translatesAutoresizingMaskIntoConstraints = false
        let navItem = UINavigationItem(title: "Create Game")
        let doneItem = UIBarButtonItem(image: UIImage(named: "Cancel"), style: .plain, target: self, action: #selector(cancelGameCreation))
        navItem.leftBarButtonItem = doneItem
        navBar.setItems([navItem], animated: false)
        self.view.addSubview(navBar)
        if #available(iOS 11, *) {
            let guide = view.safeAreaLayoutGuide
            navBar.topAnchor.constraint(equalTo: guide.topAnchor).isActive = true
        } else {
            navBar.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor).isActive = true
        }
        navBar.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        navBar.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        navBar.heightAnchor.constraint(equalToConstant: 600).isActive = true
    }

    @objc func cancelGameCreation() {
        dismiss(animated: true, completion: nil)
    }
}

Using anchors you can pin the nav you've created to the bottom of the status bar as Tommi suggested above.

That being said I'm not seeing much in the design you've shown above that couldn't be handled by adjusting the appearance of the navigation bar on a navigation controller. Is there a reason you've decided to build your own navigation bar rather than adding your view controller to a navigation controller and setting the bar button items, and title on that navigation bar. The Navigation controller should be handling the constraint issues you're seeing for you.

A quick example of that assuming the view controller is added to a navigation controller would be something like this: (Just a thought, hope this helps)

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.lightGray
        navigationItem.title = "Create Game"
        let doneItem = UIBarButtonItem(image: UIImage(named: "Cancel"), style: .plain, target: self, action: #selector(cancelGameCreation))
        navigationItem.setLeftBarButton(doneItem, animated: false)
        navigationController?.navigationBar.barTintColor = UIColor.blue
   }

    @objc func cancelGameCreation() {
        dismiss(animated: true, completion: nil)
    }
}

Upvotes: 1

Tommi Kivimäki
Tommi Kivimäki

Reputation: 116

The Navigation Bar has a fixed height. It seems that you placed it on top of the Status Bar. Try pinning the top of the Navigation Bar to the Bottom of the Status Bar with constraints.

Upvotes: 0

Related Questions