I have a trouble with a search bar only on iOS 10 (it works well on iOS 11) and I don't know what's going on, it's been several days I'm trying to solve this by myself without success so I hope someone here could tell me what I do wrong.
So, initially, my screen is like this
And when I click in the search bar, it goes like that
If I type text, it well displays the results under the search bar. I just don't know where this space between my search bar and the navigation bar comes from.
The results of my search is managed by an another viewcontroller declared like this.
let locationSearchTable = storyboard!.instantiateViewController(withIdentifier: "LocationSearchTableViewController") as! LocationSearchTableViewController
resultSearchController = UISearchController(searchResultsController: locationSearchTable)
resultSearchController?.searchResultsUpdater = locationSearchTable
A bit of my code, especially where I set up the search bar:
let searchBar = resultSearchController!.searchBar
searchBar.sizeToFit()
searchBar.placeholder = "Search for a place or address"
if #available(iOS 11.0, *) {
navigationItem.searchController = resultSearchController
resultSearchController?.dimsBackgroundDuringPresentation = true
} else {
searchBar.delegate = self
searchBar.searchBarStyle = .default
searchBar.setShowsCancelButton(false, animated: true)
searchBar.keyboardAppearance = .default
navigationController?.navigationBar.isTranslucent = false
searchBar.isTranslucent = false
resultSearchController?.dimsBackgroundDuringPresentation = false
mainView.addSubview(searchBar)
}
definesPresentationContext = true
resultSearchController?.hidesNavigationBarDuringPresentation = false
}
For others who might encounter the same issue, here is my code now and it's working.
let searchBar = resultSearchController!.searchBar
searchBar.sizeToFit()
searchBar.placeholder = "Search for a place or address"
if #available(iOS 11.0, *) {
navigationItem.searchController = resultSearchController
resultSearchController?.hidesNavigationBarDuringPresentation = false
resultSearchController?.dimsBackgroundDuringPresentation = true
} else {
searchBar.searchBarStyle = .default
searchBar.setShowsCancelButton(false, animated: true)
searchBar.keyboardAppearance = .default
navigationController?.navigationBar.isTranslucent = false
searchBar.barStyle = .default
searchBar.isTranslucent = false
resultSearchController?.hidesNavigationBarDuringPresentation = true
resultSearchController?.dimsBackgroundDuringPresentation = false
UI_mapView.addSubview(searchBar)
}
definesPresentationContext = true
let currentLocationButtonItem = MKUserTrackingBarButtonItem(mapView: UI_mapView)
self.navigationItem.rightBarButtonItem = currentLocationButtonItem
Related
Hi I have setup search controller using
private func setupSearchbar() {
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search Contacts"
self.navigationItem.searchController = searchController
self.definesPresentationContext = true
}
Everything was working fine until I add this code
UINavigationBar.appearance().isOpaque = true
UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().barTintColor = UIColor(named: "PrimaryDark")
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.font:Constants.AppTheme.Fonts.font(type: .FONT_BOLD, size: 22) ,NSAttributedString.Key.foregroundColor:UIColor.white]
UINavigationBar.appearance().largeTitleTextAttributes = [NSAttributedString.Key.font:Constants.AppTheme.Fonts.font(type: .FONT_BOLD, size: 34) ,NSAttributedString.Key.foregroundColor:UIColor.white]
Now When I tap on search bar big bottom black bar appears
How to fix this ?
View Debugging
Okay So I am able to fix this issue. I am sharing this so this might helpful to other facing the same issue.
to fix this issue. In view will appear method I have set self.extendedLayoutIncludesOpaqueBars to true
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.extendedLayoutIncludesOpaqueBars = true
}
Hope it helps to someone :)
Here is output
Remove this
UINavigationBar.appearance().isOpaque = true
UINavigationBar.appearance().isTranslucent = false
Maybe this is impossible, but I always assumed you could just throw a UISearchController instance onto any old view controller's navigationItem and get a search bar. It seems to me like no matter what I try, I can't get it to work. It's making me think this behavior is hardcoded to only work when the view controller's view property is a subclass of UIScrollView.
I hope this is just a red herring. If I missed something obvious, please help! This is infuriating.
Here's what I did:
import UIKit.UIViewController
class MainViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.hidesSearchBarWhenScrolling = false
navigationItem.searchController = {
let searchController = UISearchController()
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
searchController.obscuresBackgroundDuringPresentation = false
return searchController
}()
}
}
No search bar ever appears on screen. It just looks like a regular old navigation bar.
The UISearchController initializer should be let searchController = UISearchController(searchResultsController: nil) or replace nil with a seperate controller to display the search results.
If your viewController is in a UINavigationController stack then the above code should work (with the corrected initializer). Otherwise you will need to create a UINavigationBar and add it to the view. Then add the searchController.searchBar to the navigationItem.titleView
let navigationBar = UINavigationBar()
view.addSubview(navigationBar)
navigationBar.barTintColor = UIColor.gray
navigationBar.translatesAutoresizingMaskIntoConstraints = false
navigationBar.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
navigationBar.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
navigationBar.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
navigationBar.delegate = self
navigationBar.items = [navigationItem]
navigationItem.searchController = {
let searchController = UISearchController(searchResultsController: nil)
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
searchController.obscuresBackgroundDuringPresentation = false
return searchController
}()
navigationItem.titleView = navigationItem.searchController?.searchBar
I've implemented UISearchController and presenting it on UIViewController.
Navigation bar has search button and on clicking search, UISearchController will present from top of the screen.
Issue is, In iPhone X it looks fine but in all other devices it doesn't fit properly with spacing.
My implementation is like this :
let searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.delegate = self
searchController.searchBar.tintColor = UIColor.white
searchController.searchBar.sizeToFit()
searchController.searchBar.placeholder = SEARCH
searchController.searchBar.barTintColor = UIColor(hex: 0x2A3442)
searchController.searchBar.becomeFirstResponder()
// Search bar UI change
for subview in searchController.searchBar.subviews {
for innerSubview in subview.subviews {
if innerSubview is UITextField {
let textField = innerSubview as? UITextField
textField?.textColor = UIColor.white
textField?.borderStyle = .roundedRect
textField?.backgroundColor = UIColor(hex: 0x38465A)
}
}
}
present(searchController, animated: true)
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
}
My app needs support universal devices.
And in that, there is a view controller which have UISearchController. It can show normally in iPhone device, but in iPad, the 'cancel' button disappear.
iPhone
iPad
It is the relevant code about searchbar in the view controller, following.
func configureSearchController() {
self.collectionView!.updateConstraints()
resultsTableController = SearchBookResultTableViewController()
resultsTableController.tableView.delegate = self
searchController = UISearchController(searchResultsController: resultsTableController)
searchController.searchResultsUpdater = self
navigationItem.titleView = searchController.searchBar
searchController.searchBar.sizeToFit()
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.delegate = self
searchController.searchBar.searchBarStyle = .Minimal
searchController.searchBar.placeholder = "书籍、作者、出版社"
// stress
searchController.searchBar.showsCancelButton = true
definesPresentationContext = true
}
I want to know the reason of the problem, and in which way I can solve it.
Seek help! Thx.
I found the same situation in iOS 7 in this link. And I use the same way to solve my problem. But I don't know why the way, create a new UIView, can solve it? What's the different between them?
self.collectionView!.updateConstraints()
resultsTableController = SearchBookResultTableViewController()
resultsTableController.tableView.delegate = self
searchController = UISearchController(searchResultsController: resultsTableController)
searchController.searchResultsUpdater = self
searchController.searchBar.sizeToFit()
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.delegate = self
searchController.searchBar.searchBarStyle = .Minimal
searchController.searchBar.placeholder = "书籍、作者、出版社"
let SearchView: UIView = UIView(frame: searchController.searchBar.bounds)
SearchView.addSubview(searchController.searchBar)
navigationItem.titleView = SearchView