gpt4 book ai didi

swift - 如何从 UINavigationItem 中关闭 UISearchController?

转载 作者:行者123 更新时间:2023-11-30 11:34:25 25 4
gpt4 key购买 nike

我有一个嵌入在 UINavigationController 中的 UIViewController ,其中 UICollectionView 填充了屏幕。在 UINavigationBar 中,我有一个按钮,当选择时,该按钮会在 UINavigationBar 下显示 UISearchBar 。我这一切都工作正常。问题是,当选择 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
}

最佳答案

用途:

navigationItem.searchController = nil

关于swift - 如何从 UINavigationItem 中关闭 UISearchController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49910743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com