有一个代码可以创建搜索栏。单击此搜索栏后,然后将其与“取消”按钮一起下移。我附上了屏幕截图和代码。
@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
}
如何解决?