如何从UINavigationItem中解除UISearchController?

时间:2018-04-19 00:08:07

标签: swift uinavigationcontroller uisearchcontroller

我在UIViewController中嵌入了UINavigationController,其中UICollectionView填满了屏幕。在UINavigationBar我有一个按钮,在选中时会在UISearchBar下显示UINavigationBar。我这一切都很好。问题是,在UISearchBar上选择取消按钮时,我无法让搜索栏解除。这是我的代码:

var searchController = UISearchController()

override func viewDidLoad() {
    super.viewDidLoad()

    searchController = UISearchController(searchResultsController: nil)
    searchController.searchResultsUpdater = self
    searchController.delegate = self
    searchController.searchBar.delegate = self
    searchController.hidesNavigationBarDuringPresentation = false
    searchController.dimsBackgroundDuringPresentation = false
    searchController.searchBar.placeholder = "Search for Title"

    self.navigationItem.searchController = searchController
}

@IBAction func searchSelected(_ sender: UIBarButtonItem) {
    searchController.isActive = true
}

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    print("cancel clicked")
    searchController.isActive = false
}

1 个答案:

答案 0 :(得分:0)

使用:

navigationItem.searchController = nil