UISearchController in navigation bar could not hide properly on iOS 11 - ios

I want to hide navigation bar after a tap
navigationController?.hidesBarsOnTap = true
The navigationBar hides properly after a tap
But after adding a searchController (code below)
let searchController = UISearchController(searchResultsController: nil)
navigationItem.searchController = searchController
My view (cyan color) could not extend correctly
And I also tried rotated it. The search bar appears.

Finally found a solution
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.barHideOnTapGestureRecognizer.addTarget(self, action: #selector(barHideAction(_:)))
let searchController = UISearchController(searchResultsController: nil)
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
navigationController?.hidesBarsOnTap = true
}
#objc func barHideAction(_ guesture: UITapGestureRecognizer) {
updateFrame()
}
func updateFrame() {
if let nc = navigationController {
let isHidden = nc.isNavigationBarHidden
searchController.searchBar.superview?.isHidden = isHidden
if isHidden {
self.additionalSafeAreaInsets.top = -64 // fixed by a magic num
}
else {
self.additionalSafeAreaInsets.top = 0
}
}
}
example code

Related

Adding in SearchController causes Navigation Bar to be black

I am adding in a UISearchController in my navigation controller and when I do, the entire navigation bar goes black, instead of the color I want. It does go to the desired color when the I click on the search bar however.
Before calling setupSearch()
After calling setupSearch()
class SearchViewController: UIViewController, UISearchBarDelegate, UISearchControllerDelegate {
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
delegate()
create()
setupNav()
setupSearch()
}
func delegate() {
searchController.delegate = self
searchController.searchBar.delegate = self
}
func create() {
definesPresentationContext = true
view.backgroundColor = .backgroundColor()
}
func setupNav() {
guard let navController = navigationController else { return }
navController.navigationBar.prefersLargeTitles = false
navController.navigationBar.barTintColor = .secondaryColor()
navController.navigationBar.isTranslucent = false
navController.navigationBar.topItem?.title = "Browse"
navController.navigationBar.titleTextAttributes = [.font: UIFont.customHeaderFont(size: 23), .foregroundColor: UIColor.textColor()]
}
func setupSearch() {
let searchBar = searchController.searchBar
searchBar.backgroundImage = UIImage()
searchController.extendedLayoutIncludesOpaqueBars = true
navigationItem.searchController = searchController
}
}
if you give color to barTintColor and set isTranslucent to false,setting navigationItem.searchController will cause the bar to turn black
Solution 1, if you want to always display the searchbar
navigationItem.hidesSearchBarWhenScrolling = false
Solution 2, set navigationController backgroundColor
navigationController?.view.backgroundColor = .red
Change the background color of the View (outermost) that contains the safeArea, that works for me. The reason that you are seeing a black color is that you did not assign any color to the View background so the default one is assigned

Swift iOS -How to get keyboard to present itself at the same exact time a SearchController is presented?

Just for context my keyboard successfully appears so that's not the problem.
I have a searchButton as my rightBarButtonItem, when pressed it modally presents a vc that contains a SearchController. When the SearchController is presented the keyboard is also presented but the keyboard appears a second late, there's like a 1 second delay before it shows itself. Basically the vc appears on the scene and then the keyboard appears afterwards, I cannot get the keyboard to appear at the same time the SearchController is presented. I was on YouTube's and Vimeo's iOS apps and when I pressed their search button the keyboard is presented with the SearchController at the same exact time, there isn't a 1 second delay.
How can I get the keyboard to present itself at the same time the SearchController is presenting itself?
button to modally present SearchController:
#objc func searchButtonTapped() {
let searchVC = SearchController()
let nav = UINavigationController(rootViewController: searchVC)
present(nav, animated: true, completion: nil)
}
SearchController:
I've already tried adding searchController.isActive = true and searchController.searchBar.becomeFirstResponder() in DispatcQeue.main in viewWillAppear and viewDidAppear and it made no difference
class SearchController: UIViewController {
var searchController: UISearchController!
override func viewDidLoad() {
super.viewDidLoad()
searchController = UISearchController(searchResultsController: nil)
searchController.delegate = self
searchController.searchBar.delegate = self
searchController.searchResultsUpdater = self
searchController.searchBar.showsCancelButton = true
searchController.searchBar.placeholder = "Search"
searchController.searchBar.returnKeyType = .search
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.sizeToFit()
searchController.searchBar.tintColor = UIColor.black
definesPresentationContext = true
navigationItem.hidesBackButton = true
navigationItem.titleView = searchController.searchBar
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
searchController.isActive = true
}
// I tried both of these searchContrller delegate methods SEPERATELY but it made no difference, there's still a 1 second delay
func presentSearchController(_ searchController: UISearchController) {
DispatchQueue.main.async {
self.searchController.searchBar.becomeFirstResponder()
}
}
func didPresentSearchController(_ searchController: UISearchController) {
DispatchQueue.main.async {
self.searchController.searchBar.becomeFirstResponder()
}
}
}
Sure there is a delay.. ones this animation is completed, then keyboard appears.
present(nav, animated: true, completion: nil)
Please try this.
It will immediately open the keyboard if you would not preset view controller with an animation but if we present view controller animation it opens keyboard after the present animation is finished.
Thanks.
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
DispatchQueue.main.async {
self.searchController.searchBar.becomeFirstResponder()
}
}

Search Controller in Navigation Item confusing behaviour

So i have a searchcontroller on my navigationItem.
// View controller
var searchController = UISearchController(searchResultsController: nil)
override func viewWillAppear(_ animated: Bool) {
tableView.reloadData()
if #available(iOS 11.0, *) {
navigationController?.navigationBar.prefersLargeTitles = true
}
if employeeSearchList.count > 10 {
if #available(iOS 11.0, *) {
navigationItem.searchController = searchController
} else {
tableView.tableHeaderView = searchController.searchBar
}
} else {
if #available(iOS 11.0, *) {
let search = UISearchController(searchResultsController: nil)
navigationItem.searchController = search
navigationItem.searchController = nil
} else {
tableView.tableHeaderView = nil
}
}
UIView.animate(withDuration: 0.50, animations: {
self.view.layoutIfNeeded()
})
}
This piece of code runs perfectly. If the count is less than 10, the search controller is set, otherwise it is set to an empty search controller and then set to nil, so that it disappears from the UI.
func viewDidLoad() {
.
.
.
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.tintColor = tintColor
// Table view editing delegate -> delete operation
if employeeSearchList.count < 10 {
if #available(iOS 11.0, *) {
let search = UISearchController(searchResultsController: nil)
navigationItem.searchController = search
navigationItem.searchController = nil
} else {
tableView.tableHeaderView = nil
}
UIView.animate(withDuration: 0.50, animations: {
tableView.reloadData()
self.view.layoutIfNeeded()
})
}
.
.
.
}
Now my problem is, when i present a view controller above this one and then dismiss it, the viewWillAppear executes fine but the search controller doesn't show up. But if i push the view controller and come back, it shows up.
What are the main difference between the 2 operations ? (push/pop & present/dismiss)
It should be:
navigationController.navigationItem.searchController = searchController
instead of:
navigationItem.searchController = searchController
The latter will only take effect the next time navigationController is refreshed/loaded/whatever the appropriate term is?
When you present a view controller form other, you are presenting a new viewController hierarchy, I mean, you are outside of the previous navigation controller. If you push a new controller from the navigation controller, you are adding this to the stack, and the navigation bar will be presented.

searchResultsController with a UITableViewController hides the navigation bar, how to prevent it?

When I assign a TableViewController as the constructor for UISearchController, when the table is loaded, it hides the navigation bar (that includes title and search bar). How can I make it, so it doesn't hide it?
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
configureNavigationItem()
}
}
extension ViewController {
private func configureNavigationItem() {
let resultsController = UITableViewController(style: .plain)
navigationItem.searchController = UISearchController(searchResultsController: resultsController)
navigationItem.hidesSearchBarWhenScrolling = false
}
}
Initial Load
Tap Any Key
Found the answer:
definesPresentationContext = true
as in:
private func configureNavigationItem() {
let resultsController = UITableViewController(style: .plain)
navigationItem.searchController = UISearchController(searchResultsController: resultsController)
navigationItem.hidesSearchBarWhenScrolling = false
**definesPresentationContext = true**
}

UISearchController incorrect behavior when it is present modally

I showed UIViewController like modal style. UIViewController has UITableView. I added UISearchController to UITableView but UISearchController has incorrect behavior when it disappeared. I made many times UISearchControllers but I didn't face the this behavior.
My code
So I show the UIViewController
#objc fileprivate func selectInterlocutor(_ button: UIButton) {
let selectInterlocutorTVC = SelectInterlocutorTableViewController()
selectInterlocutorTVC.modalPresentationStyle = .overCurrentContext
selectInterlocutorTVC.modalTransitionStyle = .coverVertical
selectInterlocutorTVC.providesPresentationContextTransitionStyle = true
present(selectInterlocutorTVC, animated: true) {
}
}
I add UISearchController to tableView
fileprivate func addSearchController() {
searchController.searchBar.barStyle = .default
searchController.delegate = self
searchController.searchBar.delegate = self
searchController.searchResultsUpdater = self
searchController.searchBar.autocapitalizationType = .words
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.sizeToFit()
tableView.tableHeaderView = searchController.searchBar
}
Please look at this video that you can see this behavior.
https://www.dropbox.com/s/l3m3q8wmqoy3qv2/SearchBarBug.mov?dl=0
How can I fix it?
I removed UISearchController and I used UISearchBar and it works for me.
fileprivate func addSearchController() {
searchBar.barStyle = .default
searchBar.delegate = self
searchBar.autocapitalizationType = .words
searchBar.showsCancelButton = true
searchBar.sizeToFit()
tableView.tableHeaderView = searchBar
}

Resources