Need to show navigation title while using searchBar in navigationBar - swift

I need to show large navigation title while search with searchBar, but when I'm clicking on Searchbar or starting to type my NavigationTitle is replaced with SearchBar. I checked methods for navigationItem and didn't find any suitable.
Does anybody know how can we always show title, even while searching?
Here is my code
class FavouriteVC: UIViewController {
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.title = "Favourite"
navigationItem.searchController = searchController
navigationItem.largeTitleDisplayMode = .always
}
}
And here are shots
screen with NavTitle
screen without NavTitle

Seems like UISearchController has a hidesNavigationBarDuringPresentation that prevents the navigation bar from hiding when set to false
Include:
searchController.hidesNavigationBarDuringPresentation = false
in your viewDidLoad and if I've understood your issue right, this should do what you need.

Related

Remove extra space on top of UISearchController in UINavBar

I added a UISearchController in the navigation bar. But it has this extra top padding which I don't want. I used UIsearchController instead of a UITextField because I need its searchResultsController. Is there a way to remove the extra space, or a way to copy the searchResultsController behavior with UITextField. I created the view programmatically btw.
override func viewDidLoad() {
setupSearchBar()
}
func setupSearchBar(){
let searchController = UISearchController(searchResultsController: ResultController)
searchController.searchResultsUpdater = self
searchController.searchBar.placeholder = "Search"
navigationItem.searchController = searchController
navigationController?.navigationBar.backgroundColor = .yellow
}
Set 'automaticallyAdjustsScrollViewInsets' to false
Setting the automaticallyAdjustsScrollViewInsets value to false

UISearchController setActive does nothing

I'm trying to present a search controller in response to a button, by setting its isActive property, but it doesn't appear.
lazy var searchController: UISeachController = {
let searchController = UISearchController(searchResultsController: nil)
searchController.delegate = self
return searchController
}()
override func viewDidLoad() {
super.viewDidLoad()
definesPresentationContext = true
}
// Called when a button on the navbar is tapped
#obcj private func searchTapped() {
// The doc says you can force the search controller to appear by setting isActive,
// but nothing happens
searchController.isActive = true
// Calling present does show it, but the search bar appears behind the navbar.
// And the delegate methods are still not called
//present(searchController, animated: true)
}
func willPresentSearchController(_ searchController: UISearchController) {
// Not called, even when calling 'present'
}
It seems isActive only works if the search bar is in the view hierarchy.
// This will place the search bar in the title view, allowing you to
// have other buttons on the right of the search bar.
navigationItem.titleView = searchController.searchBar
Or
// This takes up a whole other row under title row.
navigationItem.searchController = searchController
But if you go this route, you don't need to set isActive yourself - the search controller automatically comes up when the user interacts with the search bar. I still don't understand how isActive is supposed to be used, but it does answer the original question.

Why my Searchbar stay at the top only if I use it?

I have a tableView containing a list. I use a searchbar to filter the data. When I touch chararcter in the search text, I can display filtered datas and the searchbar stay at the top when I scroll. But, if I don't use it (searchbar is empty), when I scroll, the searchbar is the first line of the list and disappear when I scroll down. You can find a part of code there :
class Liste*****Active: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating,
UISearchBarDelegate,RecupListe*****ModelProtocol {
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
ListeTab*****.tableHeaderView = searchController.searchBar
self.ListeTab*****.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
func updateSearchResults(for searchController: UISearchController) {
// If we haven't typed anything into the search bar then do not filter the results
if searchController.searchBar.text! == "" {
filtered*****s = feedItems
} else {
// Filter the results
filtered*****s = feedItems.filter { ($0 as! *****Model).nom*****!.lowercased().contains(searchController.searchBar.text!.lowercased()) } as! [*****Model] as NSArray
}
self.ListeTab*****.reloadData()
}
Nothing is defined in storyboard.
Did I forgot part of code ?
Thanks
Given your search bar is embedded in navigation bar, you need to override this behavior:
navigationItem.hidesSearchBarWhenScrolling = false
Update
You use searcher as table header.
ListeTab*****.tableHeaderView = searchController.searchBar
So that's basically an issue -- your search bar is part of the table content that's why it's getting scrolled. To avoid that you need to get your search bar out of the tableView.
Possible solutions:
Embedding searchBar into the navigation controller header view.
Adding a searchBar as a separate view on top of the table view in the view hierarchy

UISearchController shifted up when typing under UINavigationController

I embedded a viewcontroller(BlueViewController) in a navigation controller.
the blueViewController has a UISearchController that set as the blueViewController's tableView headerView.
the searchController works fine except when it's active, it shift up and hide behind the UINavigationController(or somewhere)
I've tried to add the search controller's searchBar as a subview to the viewController, or positions a UIView and assigned the searchBar to that UIView, none of these works
I have tried this link, doesn't work
Strange UISearchDisplayController view offset behavior in iOS 7 when embedded in navigation bar
here is the searchController in BlueViewController
let searchController:UISearchController = {
let controller = UISearchController(searchResultsController: nil)
controller.hidesNavigationBarDuringPresentation = false
controller.obscuresBackgroundDuringPresentation = false
controller.searchBar.inputAssistantItem.leadingBarButtonGroups = []
controller.searchBar.inputAssistantItem.trailingBarButtonGroups = []
return controller
}()
In viewDidLoad
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
definesPresentationContext = true
you can also find entire project here
https://github.com/QiquanLu/TestNavigationWithSearchController
Any hint would be appreciated, thanks!
For iOS 11 and above, you should be setting the searchController property on BlueViewController's navigationItem like so:
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.searchController = searchController
}
Don't add it as the tableView's header.

No navigation bar or any navigation bar attributes are showing up in view controller

I have a view controller that I want to have a navigation bar with a title and button to the right of the title. But for some reason the navigation bar is not showing up.
In the app delegate I added:
window?.rootViewController =
UINavigationController(rootViewController: MessagesController())
UITableViewCell.appearance().textLabel?.textColor = UIColor.white;
UITableViewCell.appearance().backgroundColor = UIColor.clear
UIApplication.shared.statusBarStyle = .lightContent
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.white]
UINavigationBar.appearance().barTintColor = UIColor(red:0.13, green:0.15, blue:0.18, alpha:1.0)
UIApplication.shared.isStatusBarHidden = false
In the viewDidLoad of the view controller I want to have the navigation bar this is my code:
override func viewDidLoad() {
super.viewDidLoad()
let dummyViewController = UIViewController()
dummyViewController.view.backgroundColor = .white
dummyViewController.navigationItem.title = "TEST"
navigationController?.navigationBar.tintColor = .white
Why is there no navigation bar and why can't I add a title with buttons to it? Any help is welcome Thank You.
I once ran into the same issue with a dark UINavigationBar. Try to add this line in your viewDidLoad:
self.navigationController?.navigationBar.barStyle = .black
This solved the issue for me. Let me know if it helps.