There is a code that creates the Search Bar . After pressing this Search Bar , it is shifted down along with the "Cancel" button. I attach screenshots and code.

enter image description here

enter image description here

 @IBAction func showSearchBar(_ sender: UIBarButtonItem) { self.navigationItem.setRightBarButtonItems([], animated: true) self.searchController = UISearchController(searchResultsController: nil) self.searchController.searchResultsUpdater = self self.searchController.searchBar.delegate = self self.searchController.delegate = self self.searchController.hidesNavigationBarDuringPresentation = false self.searchController.obscuresBackgroundDuringPresentation = true self.searchController.dimsBackgroundDuringPresentation = false self.searchController.searchBar.placeholder = "Искать продукты" self.searchController.searchBar.setShowsCancelButton(true, animated: true) self.searchController.searchBar.searchBarStyle = .minimal self.navigationItem.titleView = searchController.searchBar self.definesPresentationContext = true 

}

How to get rid of this bias?

    1 answer 1

    It's not a good idea to push searchBar in titleView. Of course, you can push it instead of the leftBarButtonItem and set a frame for it, then it will work fine. But, nevertheless, I would recommend doing as they advise Apple to insert searchController in navigationItem, namely, to replace this line

     navigationItem.titleView = searchController.searchBar 

    on this

     navigationItem.searchController = searchController 

    yes, self not needed here