user8414724 user8414724 - 6 months ago 44
Swift Question

searchBarTextDidBeginEditing not being called when i use it in navigation controller Swift 3.

I have searched for hours trying to solve my problem, but i could not find any solutions.

My problem is that i have a SearchBar inside my navigation bar. I did setup my SearchBar like this:

lazy var searchBar = UISearchBar(frame:

override func viewDidLoad() {

searchBar.delegate = self
searchBar.placeholder = "Search"

navigationItem.titleView = searchBar


When i try to call for example a function from the protocol UISearchBarDelegate it won't respond.
The function is for example:

func searchBarTextDidBeginEditing(searchBar: UISearchBar) {

print("Did run")

This function does not execute. How would i make functions from UISearchBarDelegate execute programmatically inside a navigation bar? The reason for that i do this programmatically is because i use the search bar inside a navigation bar

Answer Source

Your delegate function is just a bit different from the right one, at the parameter. It should be:

func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download