I'm trying to add another textfield for a 'Location' input to a UISearchController when the user focuses on the Search Bar, just below the Search Bar on the navigation bar.
Example of what I have and where I'd like it to go:
I've tried something like this, which doesn't work:
var searchController: UISearchController!
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
var myTextField: UITextField = UITextField(frame: CGRect(x: 0, y: 0, width: 200.00, height: 40.00))
myTextField.backgroundColor = UIColor.purpleColor()
myTextField.text = "Location"
myTextField.borderStyle = UITextBorderStyle.None
searchController.view.addSubview(myTextField)
}
Any help would be great! Thanks.
Don't know whether you found your answer or not. Let me suggest my solution.
Nothing wrong in your code. It works fine. But the reason why it doesn't show is , when you click in the search bar func searchBarTextDidBeginEditing(searchBar: UISearchBar)
has no effect on it because you might forget to set the delegate for the search bar.
I tried your code and after setting the delegate it just works fine. Also it hides behind the navbar as the Y position is 0. The solution is,
UISearchBarDelegate
to your classIn viewDidLoad()
override func viewDidLoad(){
super.viewDidLoad()
searchcontroller.searchbar.delegate = self
}
In searchBarTextDidBeginEditing
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
var myTextField: UITextField = UITextField(frame: CGRect(x: 0, y: searchController.searchBar.frame.height + 20, width: view.frame.size.width, height: 40.00))
myTextField.backgroundColor = UIColor.purpleColor()
myTextField.text = "Location"
myTextField.borderStyle = UITextBorderStyle.None
searchController.view.addSubview(myTextField)
}
Hope it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With