Search code examples
iosswiftuinavigationbar

Nav bar does not fully cover top of phone screen


I added a nav bar with no nav controller and so the nav bar is only a strip. Is there any way to make the nav bar cover the whole top of the phone screen?

enter image description here


Solution

  • Try setting a delegate object for the navigation bar. Something like;

    navigationBar.delegate = self
    

    Conform UIBarPositioningDelegate in your delegate object and attach bar to the top;

    ViewController: UIViewController, UIBarPositioningDelegate {
        
        func position(for bar: UIBarPositioning) -> UIBarPosition {
            return .topAttached
        }
        
    }