Reputation: 137
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?
Upvotes: 1
Views: 1408
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
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
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
Reputation: 11
If you're using SwiftUI, you can add this modifier to your NavigationView:
.edgesIgnoringSafeArea(.top)
Upvotes: 1
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
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