I'm try to build search function using tutorial from https://www.thorntech.com/2016/01/how-to-search-for-location-using-apples-mapkit/
I need to change his code
because as you can see from the picture if I do navigationitem.titleview the plus button disappears. I want to use searchbar when I searching but I couldn't find a way to do it
let searchBar = resultSearchController!.searchBar
searchBar.sizeToFit()
searchBar.placeholder = "Search for places"
navigationItem.titleView = resultSearchController?.searchBar
resultSearchController?.hidesNavigationBarDuringPresentation = false
resultSearchController?.dimsBackgroundDuringPresentation = true
definesPresentationContext = true
locationSearchTable.mapView = mapView
locationSearchTable.handleMapSearchDelegate = self
Instead of
navigationItem.titleView = resultSearchController?.searchBar
Try
navigationItem.searchController = resultSearchController
Related
I have UISearchBar in my app which is subview by another main view.
I have searched for many answers already on stackoverflow but seems not help for me. What i have found in stackoverflow they advice me to add self.extendedLayoutIncludesOpaqueBars = true and definesPresentationContext = true but still my UISearchBar keeps fall down every time I click on searchBar.
So, I decided to ask my own question and I am sure that you will consider this as duplicate question.
I don't use tableView.
Here is the main view of UISearchBar
private func setupSearchBarView() {
searchBarView.addSubview(searchController.searchBar)
self.searchBarView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
self.searchBarView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
self.searchBarView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
self.searchBarView.heightAnchor.constraint(equalToConstant: 56 ).isActive = true
}
Here is searchController.searchBar
func setupSearchBar(){
self.searchBarViewHeight = searchController.searchBar.frame.height
searchController.searchResultsUpdater = self
self.extendedLayoutIncludesOpaqueBars = true
searchController.searchBar.barStyle = UIBarStyle.default
searchController.searchBar.tintColor = BaseColor.colorBlack
searchController.searchBar.barTintColor = BaseColor.colorAccentDark
searchController.searchBar.sizeToFit()
searchController.searchBar.isTranslucent = true
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
}
I don't know why it always keeps shift down all the time I start filtering.
func configureSearchController()
{
resultsController.tableView.delegate = self
resultsController.tableView.dataSource = self
self.searchController = UISearchController(searchResultsController: self.resultsController)
//self.tableView.tableHeaderView = self.searchController.searchBar
self.searchController.searchResultsUpdater = self
self.searchController.dimsBackgroundDuringPresentation = false
self.searchController.searchBar.sizeToFit()
searchController.searchBar.delegate = self
self.searchController.searchBar.scopeButtonTitles = []
for subView in searchController.searchBar.subviews {
for subViewOne in subView.subviews {
if subViewOne is UITextField {
searchTextField = subViewOne as! UITextField
subViewOne.backgroundColor = UIColor.white
var currentTextFieldBounds = subViewOne.bounds
currentTextFieldBounds.size.height = 45
subViewOne.bounds = currentTextFieldBounds
break
}
}
}
extendedLayoutIncludesOpaqueBars = true
definesPresentationContext = true
}
This doesn't change height of text field unexpectedly. I also want to change height of search bar. What changes should I make here for the same to work?
IMO, The search bar size shouldn't be changed as it's the native standard provided by Apple. Also the way you use of recursively searching of textfield is not recommended and not guaranteed to work in all iOS versions.
Maybe you can try to use custom Search bar with your own text field and u can easily play with it.
I am including a search bar by code and enabling interaction using the code below, the search bar is displayed but when I click on the field editing is not enabled:
let searchTableViewController = storyboard?.instantiateViewController(withIdentifier: "SearchBarTableVC") as? SearchBarTableVC
searchTableViewController?.handleMapSearchDelegate = self
searchTableViewController?.mapView = self.mapView
searchBarController = UISearchController(searchResultsController: searchTableVC)
searchBarController?.searchResultsUpdater = searchTableVC
let searchBar = searchBarController?.searchBar
searchBar?.sizeToFit()
searchBar?.placeholder = "Digite o local"
searchBar?.tintColor = UIColor.red
searchBar!.isUserInteractionEnabled = true
searchBar?.backgroundColor = UIColor(named: "ColorTransparent")
navigationItem.titleView = searchBarController?.searchBar
searchBarController?.hidesNavigationBarDuringPresentation = false
searchBarController?.dimsBackgroundDuringPresentation = true
definesPresentationContext = true
If you're using iOS 11, the preferred way is simply to write:
navigationItem.searchController = searchController
That's all. You don't have to position the search bar or put it in any view.
I am trying to change the color of my search bar the problem is when I do the cancel button loses its color and I am unable to see it. If you have any ideas please let me know. Thank you!
Code:
self.searchController = UISearchController.init(searchResultsController: searchDataSource)
self.searchController?.obscuresBackgroundDuringPresentation = true
self.searchController?.searchBar.delegate = self
self.searchController?.searchBar.showsCancelButton = true
self.searchController?.searchBar.placeholder = "Search for items"
self.searchController?.searchBar.isTranslucent = false
self.searchController?.searchBar.barTintColor = UIColor.white
self.searchController?.searchBar.tintColor = UIColor.black
This question already has answers here:
How to left align UISearchBar placeholder text
(10 answers)
Closed 6 years ago.
here is the code for my searchBar
self.searchResultsController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.hidesNavigationBarDuringPresentation = false
controller.searchBar.sizeToFit()
controller.searchBar.placeholder = "Type your word here"
//dcontroller.view.backgroundColor = UIColor.blackColor()
controller.searchBar.barTintColor = UIColor.blackColor()
self.tableVw.tableHeaderView = controller.searchBar
return controller
})()
when i click on search bar the code becomes left align but before that it is always scattered
You can use whitespace to achieve this!!
let searchBar: UISearchBar = UISearchBar() //your searchbar
searchBar.placeholder = "Search "
Hope this will help :)