简体   繁体   中英

Search Bar Doesn't Appear When View loads - iOS

I'm having a problem when one of my views initiated. I'm trying to get the search bar to show up when the view is initiated, but it shows up when I start scrolling down. This shows up when I click on it: 在此处输入图片说明

and I'm trying to get this to show up when the view is initiated, which currently only shows up only when I start scrolling:

在此处输入图片说明

This is the code I have to set the search controller up so far:

    searchController.searchBar.scopeButtonTitles = ["Posts", "Users"]
        searchController.searchBar.delegate = self
        navigationController?.navigationItem.searchController = searchController
        navigationController?.navigationItem.searchController?.searchBar.isHidden = false
        searchController.searchResultsUpdater = self
        searchController.obscuresBackgroundDuringPresentation = false
        searchController.searchBar.placeholder = "Search"
        searchController.searchBar.isHidden = false
        searchController.searchBar.showsScopeBar = true
//        searchController.hidesNavigationBarDuringPresentation = false
        self.navigationController?.setNavigationBarHidden(false, animated: true)

        navigationItem.searchController = searchController
//        navigationController?.navigationItem.hidesSearchBarWhenScrolling = false
        definesPresentationContext = true

I've tried lots of different ways to get the search bar to appear when clicked on but I haven't been successful. Any ideas?

You need to add this line:

navigationItem.hidesSearchBarWhenScrolling = false

And if you wan't to show if from the beginning and hide it when scrolling then you need to do this:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    if #available(iOS 11.0, *) {
        navigationItem.hidesSearchBarWhenScrolling = false
    }
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    if #available(iOS 11.0, *) {
        navigationItem.hidesSearchBarWhenScrolling = true
    }
}

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