gpt4 book ai didi

ios - UISearchBar 结束编辑,隐藏键盘,但屏幕仍然变暗

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

我有一个 View Controller ,我在其中动态创建了一个 UISearchController 并将其分配给 self.navigationItem.searchController

class MyViewController: UIViewController, UISearchBarDelegate {
let searchController = UISearchController(searchResultsController: nil)

override func viewDidLoad() {
super.viewDidLoad()

navigationItem.searchController = searchController
searchController.searchBar.delegate = self

// launch the app directly into this search text box
searchController.searchBar.becomeFirstResponder()
}

func searchBarShouldEndEditing(_ searchBar: UISearchBar) -> Bool {
// I tried this
searchBar.resignFirstResponder()
// and this
searchBar.endEditing(true)
// and finally this
return true
}

}

当用户触摸搜索栏以外的任何地方并且屏幕恢复到原来的亮度时,键盘会隐藏起来。这是预期的行为。

但是,当用户点击屏幕键盘中的 [Search] 按钮时,键盘消失,但屏幕保持暗淡。没有一个 subview 可用,除非用户再次点击搜索栏,然后键盘返回。

所以简而言之,用户继续使用 View Controller 的唯一方法就是不点击 [search] 按钮,这是违反直觉的。

我错过了什么吗?

最佳答案

在 viewDidLoad() 中,添加以下行:

searchController.obscuresBackgroundDuringPresentation = false

来自文档:如果您使用相同的 View Controller 来显示可搜索内容和搜索结果,建议您将此属性设置为 false。此属性的默认值为 true。

关于ios - UISearchBar 结束编辑,隐藏键盘,但屏幕仍然变暗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56893254/

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