I have a simple TableView with a search bar added. Everything works fine, except that when the view comes up (it's the first view in the app), the search bar is focused (or appears so), so that it shows the "Cancel" button (see image). I've tried setting the tableview as first responder, calling resignFirstResponder on the search controller etc. If you tap on the search bar and then hit cancel, the focus is removed and everything's fine - the "Cancel" button goes away. Any ideas on why this is and how can I show a search bar that's not focused?
Based on the image, your textfield is not first responder and you just don't want to see cancel button
you can set searchBar.setShowsCancelButton = false
or in storyboard, uncheck Shows Cancel Button
then if you want to show cancel button during editing, check the UISearchBarDelegate
methods:
func searchBarTextDidBeginEditing
and set searchBar.setShowsCancelButton = true
func searchBarCancelButtonClicked
and set searchBar.setShowsCancelButton = false