UISearchBarController卡在NavigationBar Swift下面

时间:2019-03-19 10:08:36

标签: ios swift uinavigationbar uisearchcontroller

我正在尝试将搜索栏添加到我的应用程序的导航栏中,但是它似乎低于我当前的导航栏:

My rightBarButtonItem 'Cancel' on top of the search controllers 'Cancel' button

我要实现的目的是让SearchBar坐在“取消”按钮旁边,其中“取消”按钮仅关闭视图控制器(即未链接到搜索栏)。我要实现的样式是在搜索栏中添加一个“ x”按钮以清除文本,如下所示:

Example of what I'm trying to achieve, from the iPhone Clocks app World Clock page

这是我当前拥有的代码:

navigationItem.prompt = "Choose a City."
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(dismissPage))
let search = UISearchController(searchResultsController: nil)
search.obscuresBackgroundDuringPresentation = false
search.searchBar.placeholder = "Search"
search.searchResultsUpdater = self
search.hidesNavigationBarDuringPresentation = false
navigationItem.searchController = search
navigationItem.hidesSearchBarWhenScrolling = false
//Required for UISearchBarController
self.definesPresentationContext = true

如果有人能让我朝着写作的方向前进,我将不胜感激-我已经用尽了Google搜索的时间,因为我不确定如何描述我的要求!

非常感谢, 安迪

0 个答案:

没有答案