SearchBar in NavigationBar IOS 11

☆樱花仙子☆ 提交于 2020-01-05 04:40:50

问题


I tried using Navigation Bar Large Title and adding Search Bar to Navigation Controller. The Search Bar is displayed under the Navigation title. It should be hidden until I swipe down. I understand that the default behavior is hidden unless I set "navigationItem.hidesSearchBarWhenScrolling = false". Even I add "navigationItem.hidesSearchBarWhenScrolling = true", it doesn't work.

I created a new app to test this function. It has ViewController and Navigation Controller embedded. Below is my code:

class ViewController: UIViewController {

    let searchController = UISearchController(searchResultsController: nil)

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationController?.navigationBar.prefersLargeTitles = true
        navigationItem.searchController = searchController
        // navigationItem.hidesSearchBarWhenScrolling = true

    }
}

回答1:


I have encountered with a similar situation. I've solved like this:

1- Remove everything about searchController

2- Add UISearchBar control into your table

3- Connect this searchBar control to a IBOutlet variable and set delegate

searchBar.delegate = self

4- Use only UISearchBarDelegate (remove UISearchResultsUpdating) and implement textDidChange method

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    filter...
    reload table...
}

this table header search bar works with iOS 11 and older versions



来源:https://stackoverflow.com/questions/46511382/searchbar-in-navigationbar-ios-11

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