Hide UINavigationBar in iOS using Swift - ios

I'm pretty much confused with hiding navigation bar.
In most of the UIViewController I'm using:
self.navigationController?.navigationBar.isHidden = true
But some times UINavigationBar is not hidden or visible.
Instead, if I use:
self.navigationController?.isNavigationBarHidden = true
I can hide or show UINavigationBar.
What's the difference between above properties?

Related

Visible UISearchBar changes UINavigationBar background color

A tableview controller is embedded into a navigation controller.
I programmatically added a search bar to the tableview controller's navigation bar. I only changed the navigation bar Background color into something different than Default (purple) - all the rest I left default.
class TableViewController: UITableViewController {
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.hidesSearchBarWhenScrolling = true
navigationItem.searchController = searchController
}
}
Code above is reduced to bare minimum for demonstration purpose.
All done with Xcode 11 (11A420a).
I ran the project in iOS 12.0 and 13.0 simulators and devices.
iOS 13.0
The search bar shows upon start.
Navigation bar background color is correctly presented.
While scrolling, navigation bar background color remains correct.
With iOS 13.0, all works as expected!
iOS 12.0
The search bar doesn't show upon start.
Navigation bar background color is correctly presented.
While scrolling, navigation bar background color goes white as soon search bar is visible.
I tried to change all kind of color setting in storyboard as well as properties programmatically. I didn't succeed in changing the navigation bar background color when search bar is visible.
It seems (?!) that the navigation bar foreground looses transparency when search bar becomes visible.
If I use a Bar Tint color of the navigation bar (!= Default), all works as expected (and as with iOS 13.0), but I loose the gradient effect, which I would like to keep.
What did I miss?
How can I avoid this?
Bug?
I had some luck with the navigationItem.scrollEdgeAppearance property when facing a similar problem. For example:
vc.navigationItem.scrollEdgeAppearance?.backgroundColor = .red
This is only available on iOS 13 though.
Here's what I ended up doing to get correct colors in the navigation bar while allowing the search controller's scroll bar to hide during scroll:
if #available(iOS 13.0, *) {
let blurEffect = UIBlurEffect(style: .systemUltraThinMaterial)
navbar.standardAppearance.backgroundEffect = blurEffect
navbar.standardAppearance.backgroundColor = appMainColor.withAlphaComponent(0.75)
navbar.standardAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
navbar.compactAppearance = nil
navbar.scrollEdgeAppearance = navbar.standardAppearance.copy()
navitem.standardAppearance = nil
navitem.compactAppearance = nil
navitem.scrollEdgeAppearance = nil
}
I don't know if it is the look you're going for but I found if you disable hideSearchBarWhenScrolling the background stops changing color. However, the search bar is always there.
Add this to viewDidLoad():
navigationItem.hidesSearchBarWhenScrolling = false

How to hide UISearchController inside of navigation bar

I want to dismiss/hide UISearchController from navigation bar when I switch tabs, I tried many things, if I set it to nil a black layer is shown in the search space. The settings view controller should not present the UISearch any thoughts?
Without seeing your code, this is what I would do if I wanted to hide my search bar. Use the following code to hide searchBar:
searchBar.isHidden = true
view.backgroundColor = .orange
//.clear? Or whatever colour you want to be there in the background.
//In viewDidLoad put
searchBar.isHidden = false
I was adding only one Navigation Controller to all tabs, I changed it, now for each tab I have an independent navigation controller, so it is solved.

Completely disable the Navigation Controller

I have used the 'Embed In' navigation controller within the storyboard. Is there a way to completely disable the whole navigation bar? I have tried the code below, but when there is a swipe, the navigation bar appears again. I want to be able to completely hide the whole bar until a condition is met. Is there a way to do this?
self.navigationController?.hidesBarsOnSwipe = true
self.navigationController?.hidesBarsOnTap = true
I have tried a custom navigation bar, but I couldn't get a nice scroll effect like with the default navigation bar.
To hide the navigationBar just do:
self.navigationController?.navigationBar.isHidden = true
Add this row in your viewDidLoad.
You can use this in viewDidLoad:
self.navigationController?.setNavigationBarHidden(true, animated: false)

UISearchController and UISearchBar are not animating properly and are changing the Status Bar

I'm working in a fresh project with Xcode 7.2, built against iOS 9.2, and testing on an iPhone 6. This project is less than a week old with a minimal framework primarily defined in a storyboard.
The storyboard has a float of UINavigationController > UITabBarController > UITableViewController, which is the one with the search code, defined below.
In my Info.plist, I have Status bar style set to UIStatusBarStyleLightContent, Status bar is initially hidden set to YES, and View controller-based status bar appearance set to No.
In the storyboard, I do not have a UISearchBar or anything else on the UITableViewController other than the prototype UITableViewCell.
Nowhere in my simple app do I override preferredStatusBarStyle and on the storyboard I have made sure all the Status Bar values are set to Light Content for every controller, just to be safe.
The UITableViewController has the following code in viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
searchResultsController = MovieSearchResultsController()
searchResultsController.tableView.delegate = self
searchController = UISearchController(searchResultsController: searchResultsController)
searchController.delegate = self
searchController.dimsBackgroundDuringPresentation = true
searchController.hidesNavigationBarDuringPresentation = true
searchController.searchResultsUpdater = self
searchController.searchBar.delegate = self
searchController.searchBar.tintColor = UIColor.whiteColor()
searchController.searchBar.sizeToFit()
self.tableView.delegate = self
self.tableView.tableHeaderView = searchController.searchBar
self.definesPresentationContext = true
}
When the UISearchBar is selected, the entire view shifts upwards, covering the UINavigationBar. This is great, except the status bar changes to black text and the UISearchBar disappears.
I'm pulling my hair out and have been pouring over this all day. I've read so many answers on Stack and nothing seems to work.
When I run Apple's UISearchController demo, it works exactly as intended. I've tried to replicate this exactly and I'm having no luck. I've also tried picking apart the demo and inserting my own code and it seems to work fine, short of completely redoing the project in this container, which seems highly unnecessary.
For the time being, I've just set searchController.hidesNavigationBarDuringPresentation to false and removed self.definesPresentationContext.
My Goal:
The status bar will remain with a white text color and the background color of my UINavigationBar.
The UINavigationBar will hide as the UISearchBar animates, shifting the view up.
The animation will flow smoothly, just like in the Apple demo. No jumpiness or strange delay.

IOS8 UISearchController display searchbar directly

I am trying to display a simple search screen for my iphone app using a UISearchController.
I used a UITableViewController and a UISearchController that I embedded inside a UINavigationController
This get me the following screen (state 1)
When user click on the search field I get the screen below (state 2):
The search bar display nicely at the top of the screen below the status bar.
What I want to do is to get state 2 straight away without going to state 1
I tried to add this code
presentViewController(searchController, animated: true, completion: nil)
but it either does not show the searchbar if animated is false or display it as I want but after an unnecessary transition that temporaryly shows the navigation bar
Ok I just found how to do it reading this answer UISearchController in a UIViewController
I just need to add the searchbar like this to get the desired effect
searchController.hidesNavigationBarDuringPresentation = false
// tableView.tableHeaderView = searchController.searchBar
navigationItem.titleView = searchController.searchBar

Resources