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?
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?
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
}
}
If you're using SwiftUI, you can add this modifier to your NavigationView:
.edgesIgnoringSafeArea(.top)
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.
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