我对搜索栏和范围栏有问题。基本上,只要我点击“取消”并设置searchBar.showsScopeBar = false
,当下次搜索栏出现时,就会留下这个奇怪的空间。
这里有些代码:
override func viewDidLoad() {
super.viewDidLoad()
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.hidesNavigationBarDuringPresentation = true
controller.searchBar.searchBarStyle = UISearchBarStyle.Minimal
controller.searchBar.delegate = self
controller.searchBar.sizeToFit()
self.tableView.tableHeaderView = controller.searchBar
return controller
})()
以及显示和隐藏范围栏背后的逻辑:
func searchBarShouldBeginEditing(searchBar: UISearchBar) -> Bool {
self.resultSearchController.searchBar.showsScopeBar = true
self.resultSearchController.searchBar.scopeButtonTitles = ["All", "Hot", "Active", "Warm"]
self.resultSearchController.searchBar.sizeToFit();
return true
}
func searchBarShouldEndEditing(searchBar: UISearchBar) -> Bool {
self.resultSearchController.searchBar.showsScopeBar = false
return true
}
这就是正在发生的事情:
我可以让scopebar栏一直可见,但是当创建期间在searchbar之前弹出时,当我进入详细信息时,它会落在下面。所以比现在更糟。
最佳答案
您可以在创建uisearchcontroller本身的过程中创建范围栏(只有在搜索栏处于活动状态时才可见)。不需要手动显示或隐藏范围栏。
override func viewDidLoad() {
super.viewDidLoad()
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.hidesNavigationBarDuringPresentation = true
controller.searchBar.searchBarStyle = UISearchBarStyle.Minimal
controller.searchBar.delegate = self
controller.searchBar.sizeToFit()
controller.searchBar.scopeButtonTitles = ["All", "Hot", "Active", "Warm"]
definesPresentationContext = true
self.tableView.tableHeaderView = controller.searchBar
return controller
})()