UISearchController: searchBar and scopeBar overlap on first touch event

為{幸葍}努か 提交于 2019-12-03 08:27:44
Rich16

I just spent three days working on this same issue going through all of the posts I could find and just finally found the solution on the Apple Dev forum so thought I would post an update to help the next person that encounters this issue.

The resolution is to make sure that the showsScopeBar property on the search bar belonging to a UISearchController is set to false, i.e. UISearchController.searchBar.showsScopeBar = false in Swift.

The issue seems to be that Apple's design intent is for the showsScopeBar property to be used with standalone search bars not search bars controlled by a UISearchController. Unfortunately this is not called out in the class documentation. Bad design decision in my opinion but it is what it is.

The discussion is in the following thread https://devforums.apple.com/thread/235803 (dead link as of 2018-01-26).

Best of luck.

This appears to be a known defect. There are several radr entries such as http://www.openradar.me/20702394 that refer to similar issues and a workaround by using sizeToFit()

The workaround they suggest works, but only when it was applied within viewDidLayoutSubviews. i.e. after all of the views were laid out.

override func viewDidLayoutSubviews() {
    self.searchController.searchBar.sizeToFit()
}

Update for swift 3:

searchController.searchBar.showsScopeBar = false

Update for swift 4:

It seems that in swift 4 and ios 11 the search bar got changed. With the method showed above the scope bar will be inside the search bar in some cases.

if #available(iOS 11.0, *) {
        navigationItem.searchController = searchController
        navigationItem.hidesSearchBarWhenScrolling = false
    } else {
        tableView.tableHeaderView = searchController.searchBar
    }

This fixed it for me. I use the the form shown above if ios 10 is available. If ios 11 is available i change my technic and set the navigation view controller to the search controller. You will notice it looks exactly as on ios 10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!