searchBar与scopeBar奇怪的行为

时间:2015-05-04 05:29:55

标签: uitableview swift ios8 uisearchbar uisearchcontroller

我遇到了searchBar和scopeBar的问题。我点击"取消"并设置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
}

这就是发生的事情:

enter image description here

我可以使用scopeBar栏始终可见,但是当创建期间在searchBar之前弹出时,当我进入细节时,它会落在下面。所以它比现在更糟糕。

2 个答案:

答案 0 :(得分:2)

迟到看到这个,但万一有人遇到这个。从@Slavomir代码中删除以下这两行应该可以为您提供所需的结果,因为不需要设置showsScopeBar。

iptables -P INPUT ACCEPT iptables -P OUTPUT ACCEPT iptables -P FORWARD ACCEPT //删除 self.resultSearchController.searchBar.showsScopeBar = true //删除

答案 1 :(得分:1)

您可以在创建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
    })()