Navigation bar not covering status bar on iPhone X - ios

I have a simple view controller with a view (map) pinned to its superview. The view is embedded on a navigation controller, but the navigation bar starts under the safe area (below status bar) on iPhone X. Ideally, I would like the bar to be under the status bar (not below it), when rendered on an iPhone X. How can I go about doing this?
This is how the bar is being set up to make it semi transparent:
//Design Set Up
let navBar = UINavigationBar.appearance()
navBar.setBackgroundImage(UIImage(), for: .default)
navBar.backgroundColor = UIColor(displayP3Red: 0, green: 0, blue: 0, alpha: 0.5)

You should embed the view controller in a UINavigationController, not use just a UINavigationBar. That way you will automatically get a navigation bar with the navigation controller and it will extend fully to the top of the screen.
https://developer.apple.com/documentation/uikit/uinavigationcontroller
You can add it in a Storyboard or if you're using code, something like:
let mapViewController = UINavigationController(rootViewController: MapViewController())
(assuming your map is called MapViewController)

Related

How to show a custom view on top of navigation bar in Swift?

So the way I understand navigation bar (navigation item) is that it has three locations you can modify, which is left (leftBarButtomItem), middle (titleView), and right (rightBarButtonItem).
Now what I'm going to achieve is that I want to just add a simple progress bar line at the very bottom of navigation bar, but still inside navigation bar. I want to make this like an extension of navigation bar that I can reuse on other screens. But I want that left, middle, and right "views" are still working like usual. e.g. I don't want that if I change the title view content manually in other view controller, then the line disappears / stops working for that other view controller. So this will feel like an independent overlay added on top of navigation bar as subview, separated from leftBarButtonItem, titleView, and rightBarButtonView, sort to speak.
Is it possible to do that in navigation item?
This is a example of how to add a Image instead of a Title String
fileprivate func setupTitle(){
let logo = UIImage(named: "my_incredible_logo")
let imageView = UIImageView(image:logo)
imageView.contentMode = .scaleAspectFit
self.navigationItem.titleView = imageView
self.navigationController?.navigationBar.prefersLargeTitles = false
}

Status bar left white rectangle after rotate in landscape mode

I have VC with UIView with hidden navigation bar with
self.navigationController?.setNavigationBarHidden(true, animated: animated)
At top VC have my view with constraint View.top = SafeArea.Top
When in portrait mode - all is ok. But when rotate to landscape mode status bar left in VC like white rectangle.
I don't need status bar in landscape. How to remove it? And why statusbar left this rectagle?
I have implemented the code according to your scenario it's working fine for me. I thinks this is problem with xcode/compiler. You can do:
set the main view clipToBounds property to true.
Hide navigation bar from storyboard: Select NavigationController, open identity inspector, untick the shows Navigation Bar option
SORRY!
I find the reason.
This is a custom view, that used for coloring statusbar.
Example of adding this view.
let statusBarView = UIView(frame: UIApplication.shared.statusBarFrame)
let statusBarColor = UIColor(r: 240, g: 243, b: 245)
statusBarView.backgroundColor = statusBarColor
view.addSubview(statusBarView)
Thank you everyone!

Styling UINavigationBar to match the style in UINavigationController

I have a tap bar that with 3 items. The first item is a UISplitView showing a UIViewController with a UITableView. The other two items are simple UIViewControllers embedded in a UINavigationController.
The two UIViewControllers embedded in a UINavigationController have a navigation bar with their title provided by the UINavigationController and "default" color.
I added a UINavigationBar to the UINavigationController in the first item but it has different color, it is white (Messages in the gif):
So I found out the default color of the UINavigationBar embedded in a UINavigationController and set it in code
navigationBar.barTintColor = UIColor(colorLiteralRed: (247/255), green: (247/255), blue: (247/255), alpha: 1)
The navigation bar changes color but the status bar remains white:
I tried the accepted answer from How to change Status Bar text color in iOS 7 but it did not help.
So how do I style the UINavigationBar in my view controller so it exactly matches the default one in navigation controller?
I think we worked together at Fuerte International a couple of years ago!
The status bar doesn't have a colour, it should just be the colour of your background view. Change the colour of that and you should see the status bar change too?

UINavigationBar isTranslucent false but maintain transparency

I currently have a project set up where for during sign up I am using a UINavigationController to manage the view controllers, and as part of the design I set the UINavigationBar to be transparent with the following code:
let navBar: UINavigationBar! = self.navigationController?.navigationBar
navBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
navBar.shadowImage = UIImage()
navBar.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0)
navBar.tintColor = UIColor.white
navBar.barTintColor = UIColor.clear
This allows me to use the navigation controller's heirarchy to manage the backwards/forwards movement, and works fine, except that when I show a view controller, any view that I have (set up through Autolayout) does a "jumping" motion to get into its correct position (just like this question's video https://vid.me/9kB5). Some searching led me to try the following 2 solutions
1.) Uncheck Extend Edges - Under Top Bars in my storyboard view controller
2.) Set translucency for the nav bar to false
navBar.isTranslucent = false
This solves the jumping motion and causes the views to be in place when they load, but the problem with this is that the navigation bar now becomes the barTintColor (which for UIColor.clear ends up being black). It seems I am unable to keep both transparency and translucency at the same time. A lot of similar questions on Stack have navigation bars with set colors and not transparent.
The next approach I thought of would be that I would have to get rid of the UINavigationController, and instead implement back buttons on each of my sign up view controllers, but I was hoping to try and tackle this with the navigation controller.
Is it possible to have a UINavigationBar have translucency set to false but maintain transparency?
EDIT: Here's a picture of what the nav bar looks like with the isTransluscent = false code:
Try changing "alpha" property. Make sure that everything you do with navigation bar you do on the main thread.
Solution 2: you can set specific UIImage for your navigation bar background.

Search bar bug - iOS

In my app I have this kind of bug:
When I scroll down, the content of table is seen above the search bar. What can be the reason?
override func viewDidLoad() {
super.viewDidLoad()
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
controller.searchBar.barTintColor = UIColor(red: 52.0/255.0, green: 73.0/255.0, blue: 94.0/255.0, alpha: 1.0)
self.tableView.tableHeaderView = controller.searchBar
return controller
})()
self.tableView.reloadData()
}
The iOS Human Interface Guidelines list 3 methods to prevent content from showing through the status bar.
Use a navigation controller to display content. A navigation controller automatically displays a status bar background and it
ensures that its content views don’t appear behind the status bar. (To
learn more about navigation controllers, see Navigation Controllers.)
Create a nondistracting custom image—such as a gradient—and display it behind the status bar. To ensure that the image stays
behind the status bar, you could use a view controller to keep the
image above a scrolling view or you could use a scrolling view to keep
it pinned to the top.
Position content to avoid the status bar area (that is, the area defined by the app’s statusBarFrame property). If you do this, you
should use the window’s background color to provide a solid color
behind the status bar.
You should make the status bar not translucent.
The status bar's color is a property of UIApplication. You need to set it to a valid UIStatusBarStyle.
The new UISearchController has been designed to work with a UITableView and a UINavigationBar. Its functionality depends on your navigation bar being present. when the search bar has content in its text field, it will be latched on top of the page under the navigation bar. well, you don't have any navigation bar, it will latch to the top view guide and it always takes into account the status bar (if present) I guess the only way you can resolve your problem without an actual navigation bar is to add a view under status bar and make it opaque just like your search bar. it's a bit hacky but it's alright in my book.

Resources