How to redraw NavigationBar to show SearchController on button press? - swift

I am using a button in my navigationItem.title, which when pressed, I would like to display the navigationItem.searchController.
When cancel is pressed on the search bar, I would like to dismiss the search controller, and redraw the navigation bar.
var universalSearchController = UISearchController(searchResultsController: nil)
override func viewWillAppear(_ animated: Bool) {
// Set search delegate and who will be updating the results
universalSearchController.searchBar.delegate = self
universalSearchController.searchResultsUpdater = self
universalSearchController.delegate = self
definesPresentationContext = true
titleButton.addTarget(self, action: #selector(titleButtonTapped), for: .touchUpInside)
}
Basically everything I have tried is all in here:
#objc func titleButtonTapped() {
// Set the searchController
navigationItem.searchController = universalSearchController
// Below are attempts at redrawing the navigation bar
universalSearchController.searchBar.becomeFirstResponder()
universalSearchController.searchBar.isHidden = false
// Try to refresh by showing/hiding
self.navigationController?.isNavigationBarHidden = true
self.navigationController?.isNavigationBarHidden = false
universalSearchController.view.setNeedsLayout()
universalSearchController.view.layoutIfNeeded()
universalSearchController.view.setNeedsDisplay()
universalSearchController.view.reloadInputViews()
view.setNeedsDisplay()
view.setNeedsLayout()
view.layoutIfNeeded()
view.reloadInputViews()
navigationController!.navigationBar.setNeedsDisplay()
navigationController!.navigationBar.setNeedsLayout()
navigationController!.navigationBar.layoutIfNeeded()
navigationController!.navigationBar.reloadInputViews()
navigationController!.view.setNeedsDisplay()
navigationController!.view.setNeedsLayout()
navigationController!.view.layoutIfNeeded()
navigationController!.view.reloadInputViews()
}
For hiding the searchController:
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
// Get rid of the searchController
navigationItem.searchController = nil
universalSearchController.searchBar.isHidden = true
universalSearchController.searchBar.resignFirstResponder()
// Try to refresh by showing/hiding
self.navigationController?.isNavigationBarHidden = true
self.navigationController?.isNavigationBarHidden = false
navigationController?.navigationBar.backgroundColor = .red
universalSearchController.view.setNeedsLayout()
universalSearchController.view.layoutIfNeeded()
universalSearchController.view.setNeedsDisplay()
universalSearchController.view.reloadInputViews()
view.setNeedsDisplay()
view.setNeedsLayout()
view.layoutIfNeeded()
view.reloadInputViews()
navigationController!.navigationBar.setNeedsDisplay()
navigationController!.navigationBar.setNeedsLayout()
navigationController!.navigationBar.layoutIfNeeded()
navigationController!.navigationBar.reloadInputViews()
}
The weird thing is that if I present a view over my current table view, and then dismiss it, the navigation bar is there.

Related

UISeachController Delegate methods not called, searchbar can't become first responder

I'm having some difficulty getting the searchbar in my searchcontroller to become firstResponder. I've noticed the delegate methods are not being called, but the searchbar works as intended when I'm typing to filter a list of users.
Definition of searchcontroller:
lazy var searchController: UISearchController = {
let searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search"
return searchController
}()
Setting it up:
private func setupSearchController() {
self.navigationItem.searchController = searchController
searchController.definesPresentationContext = true
searchController.delegate = self
searchController.isActive = true
searchController.searchBar.delegate = self
searchController.searchBar.becomeFirstResponder()
}
I tried this suggestion from another SO question but the delegate method isn't being called:
func didPresentSearchController(searchController: UISearchController) {
UIView.animate(withDuration: 0.1, animations: { () -> Void in }) { (completed) -> Void in
searchController.searchBar.becomeFirstResponder()
}
}
The problem is you are trying to access UI element(searchbarcontroller) before UI is completely loaded.
This can be done in 2 ways
Use main queue to show keypad
private func setupSearchController() {
self.navigationItem.searchController = searchController
searchController.definesPresentationContext = true
searchController.delegate = self
searchController.isActive = true
searchController.searchBar.delegate = self
DispatchQueue.main.async {
self.searchController.searchBar.becomeFirstResponder()
}
}
With this approach keypad will be only show at one time in viewDidLoad
Show keypad in viewDidAppear
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
DispatchQueue.main.async {
self.searchController.searchBar.becomeFirstResponder()
}
}
With this approach keypad will be always shown whenever screen appears.

Search Bar Doesn't Appear When View loads - iOS

I'm having a problem when one of my views initiated. I'm trying to get the search bar to show up when the view is initiated, but it shows up when I start scrolling down.
This shows up when I click on it:
and I'm trying to get this to show up when the view is initiated, which currently only shows up only when I start scrolling:
This is the code I have to set the search controller up so far:
searchController.searchBar.scopeButtonTitles = ["Posts", "Users"]
searchController.searchBar.delegate = self
navigationController?.navigationItem.searchController = searchController
navigationController?.navigationItem.searchController?.searchBar.isHidden = false
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search"
searchController.searchBar.isHidden = false
searchController.searchBar.showsScopeBar = true
// searchController.hidesNavigationBarDuringPresentation = false
self.navigationController?.setNavigationBarHidden(false, animated: true)
navigationItem.searchController = searchController
// navigationController?.navigationItem.hidesSearchBarWhenScrolling = false
definesPresentationContext = true
I've tried lots of different ways to get the search bar to appear when clicked on but I haven't been successful. Any ideas?
You need to add this line:
navigationItem.hidesSearchBarWhenScrolling = false
And if you wan't to show if from the beginning and hide it when scrolling then you need to do this:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if #available(iOS 11.0, *) {
navigationItem.hidesSearchBarWhenScrolling = false
}
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if #available(iOS 11.0, *) {
navigationItem.hidesSearchBarWhenScrolling = true
}
}

Search bar not aligned next to back button in navigation bar

I used a custom navigation controller class to remove the back button in my navigation bar, with the following:
class CustomNavigationController: UINavigationController, UINavigationControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
viewController.navigationItem.backBarButtonItem = UIBarButtonItem(title: " ", style: .plain, target: nil, action: nil)
}
}
In one of my VCs, I also add a search controller to the navigation bar with the following:
private func configureSearchController() {
self.searchController = UISearchController(searchResultsController: nil)
self.searchController.searchResultsUpdater = self
self.searchController.delegate = self
self.searchController.searchBar.delegate = self
self.searchController.hidesNavigationBarDuringPresentation = false
self.searchController.dimsBackgroundDuringPresentation = true
self.navigationItem.titleView = searchController.searchBar
self.definesPresentationContext = true
}
However, it seems like the search bar is still being offset by some blank space where the title would be. I'd image the search bar should be aligned next to the back button, but there is the back button, space with blank title, then search bar.
How can I get the search bar aligned next to the back button instead of spaced because it seems there is some room for the blank title. Also, as a note, tapping on that blank space moves me back to the previous VC, although there is no actual title there.
EDIT:
View hierarchy, looks like the back button container view itself takes up a good amount of space
Try adding this in viewDidLoad
let backButton = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
backButton.setBackButtonTitlePositionAdjustment(UIOffsetMake(-1000, -1000), for: .default)
navigationItem.backBarButtonItem = backButton

disable hiding searchBar when scrolling

I am coding to setup search bar (not from storyboard)
here are my codes that related to searchbar:
var searchController = UISearchController(searchResultsController: nil)
func setupNavBar() {
self.navigationItem.title = "Tools"
self.navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.hidesSearchBarWhenScrolling = false
}
func configureSearchController() {
filteredData = toolsList.allTools
searchController.searchResultsUpdater = self
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.searchBarStyle = .minimal
searchController.searchBar.tintColor = .purple
tableView.tableHeaderView = searchController.searchBar
searchController.searchBar.placeholder = "Search for tools"
}
override func viewDidLoad() {
super.viewDidLoad()
setupNavBar()
tableViewConfigurations()
configureSearchController()
}
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
isSearching = true
tableView.reloadData()
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
isSearching = false
tableView.reloadData()
}
although with this line of code i ask to disable hiding search bar when scrolling, it is still hiding.
navigationItem.hidesSearchBarWhenScrolling = false
May i ask you to guide me to how i can disable it?
Thank you very much
Looking at Apple's docs I found this:
You must configure the searchController property for this property
[hidesSearchBarWhenScrolling] to have any effect. The navigation
controller hides and shows only the search bar provided by the search
controller in that property.
I'd try adding searchController to navigationItem.
#very_supercharged thanks 😊 i have transferred searchBar from table view to navigationBar
i have changed:
tableView.tableHeaderView = searchController.searchBar
to
navigationItem.searchController = searchController
And now, searchBar does not hide when scrolling.

Set action to cancel button inside an uisearchController

let searchController = UISearchController(searchResultsController: nil)
uiView.addSubview(searchController.searchBar)
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
How can i add action to the cancel button of the search bar. i want to reload table's data once the user press the cancel button.
You need to set the delegate of the UISearchController like so:
searchController.searchBar.delegate = self;
Then you can add the method below to your code (which is triggered when the cancel button is pressed):
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
}