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.
Related
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
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.
In my iOS app I have a navigation bar composed of a UISearchBar and a UIButton in a UIStackView. Since I can't storyboard a UISearchController I have a blank UIView in the stack view and I'm adding the UISearchBar as a subview. Here's what my storyboard looks like:
Here's my code to add the search bar
override func viewDidLoad() {
super.viewDidLoad()
configureSearchController()
print(wrapperView.bounds)
print(searchController.searchBar.bounds)
}
func configureSearchController() {
searchController = UISearchController(searchResultsController: nil)
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search here..."
searchController.searchBar.sizeToFit()
searchController.searchBar.isTranslucent = false
searchController.searchBar.delegate = self
searchController.delegate = self
searchController.searchBar.barTintColor = addButton.backgroundColor
wrapperView.addSubview(searchController.searchBar)
let color = addButton.backgroundColor
searchController.searchBar.layer.borderWidth = 1
searchController.searchBar.layer.borderColor = color?.cgColor
searchController.searchBar.trailingAnchor.constraint(equalTo: addButton.leadingAnchor)
}
But the search bar goes past the UIButton, as shown here:
How can I get the UISearchBar to end at the UIButton like this?
Try replacing:
searchController.searchBar.trailingAnchor.constraint(equalTo: addButton.leadingAnchor)
with:
searchController.translatesAutoresizingMaskIntoConstraints = false
wrapperView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subview]-0-|", options: nil, metrics: nil, views: ["subview": searchController]))
wrapperView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[subview]-0-|", options: nil, metrics: nil, views: ["subview": searchController]))
EDIT:
Otherwise try with a UISearchController:
searchController.searchBar.setImage(<image_name>, forSearchBarIcon: .Bookmark, state: .Normal)
this will allow you to add a custom button near the search bar.
I experienced the same issue, and I was able to fix it by calling searchController.searchBar.sizeToFit() after adding it to my view (right after the addSubview() line). You might have to run wrapperView.layoutIfNeeded() before, so something like this:
wrapperView.addSubview(searchController.searchBar)
wrapperView.layoutIfNeeded()
searchController.searchBar.sizeToFit()
Hope it helps!
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 :)