Adding searchBar to navigationBar in swift

I am trying to add a searchBar to a navigationBar. I studied a little, and this is what I have done so far:

The problem is that this code is not displayed in the navigation block.

searchBar = UISearchBar(frame: CGRectMake(0, 0, 320, 44)) searchBar?.delegate = self searchBar?.showsCancelButton = true searchController = UISearchDisplayController() searchController?.delegate = self searchController?.searchResultsDelegate = self searchController?.displaysSearchBarInNavigationBar = true 
+5
source share
2 answers

Try this code that worked for me:

  lazy var searchBars:UISearchBar = UISearchBar(frame: CGRectMake(0, 0, 200, 20)) override func viewDidLoad() { super.viewDidLoad() var leftNavBarButton = UIBarButtonItem(customView: searchBars) self.navigationItem.leftBarButtonItem = leftNavBarButton } 
+6
source

Try this

 lazy var searchBar = UISearchBar(frame: .zero) override func viewDidLoad() { super.viewDidLoad() navigationItem.titleView = searchBar} 
0
source

Source: https://habr.com/ru/post/1204740/


All Articles