gofish
gofish

Reputation: 137

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

Upvotes: 1

Views: 1408

Answers (6)

Muaz Talha BULUT
Muaz Talha BULUT

Reputation: 11

If you want the navigation bar to merge with the status bar, you probably haven't set the apperance.

private func setApperance() {
        let apperance = UINavigationBarAppearance()
        apperance.configureWithOpaqueBackground()
        apperance.backgroundColor = .mainBackGround
        
        navigationController?.navigationBar.standardAppearance = apperance
        navigationController?.navigationBar.scrollEdgeAppearance = apperance
        navigationController?.navigationBar.prefersLargeTitles = false
    }

Upvotes: 0

Saurabh Bisht
Saurabh Bisht

Reputation: 424

This works for me ..

In Swift -

You can use the UINavigationBarAppearance() for iOS 15* where your setting your root navigation as below:

 var navigationViewController = UINavigationController()
    navigationViewController.navigationBar.backgroundColor = .purple
    let appearance = UINavigationBarAppearance()
    appearance.backgroundColor = UIColor.blue
    appearance.titleTextAttributes = [.foregroundColor: UIColor.white]
    UINavigationBar.appearance().scrollEdgeAppearance = appearance

In SwiftUI -

.edgesIgnoringSafeArea(.top)

Upvotes: 0

Desdenova
Desdenova

Reputation: 5377

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
    }
    
}

Upvotes: 0

Gustavo Tiecker
Gustavo Tiecker

Reputation: 11

If you're using SwiftUI, you can add this modifier to your NavigationView:

.edgesIgnoringSafeArea(.top)

Upvotes: 1

Fabio
Fabio

Reputation: 5648

Simply add a dummy view on top of your dummyNavBar, declare your top view under your controller declaration class:

let topView = UIView()

now in viewDidLoad set topView background color and add constraints:

topView.backgroundColor = .white // the background must be the same of your dummyNavBar
topView.translatesAutoresizingMaskIntoConstraints = false

view.addSubview(topView)
topView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
topView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
topView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
topView.bottomAnchor.constraint(equalTo: yourDummynavBar.topAnchor).isActive = true

Upvotes: 0

Mathew Miller
Mathew Miller

Reputation: 1

Without a navigation controller, you take on responsibility for sizing and positioning the bar. Please try adding some constraints pinning the top, leading, and trailing anchors to the top, leading and trailing anchors of the superview. Next, add a constraint pinning the bottom of your navigation bar to the bottom of the safe area with a constant of say 44 pts, whatever height you desire.

Upvotes: 0

Related Questions