简体   繁体   中英

SearchBar in NavigationBar IOS 11

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

    }
}

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 method 方法

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM