简体   繁体   中英

UISearchController in navigationBar

I'm trying to use the new UISearchController in my tableViewController.

However I'm a bit confused on how it can move up in the navigationController when I press the searchBar like it did with the old searchDisplayController?

At the moment it just stay in the tableHeader.

Here is my code:

    self.teamSearchController = ({
        let controller = UISearchController(searchResultsController: nil)

        controller.searchBar.searchBarStyle = UISearchBarStyle.Minimal
        controller.dimsBackgroundDuringPresentation = false
        controller.searchBar.sizeToFit()
       controller.searchBar.showsScopeBar = true
        self.tableView.tableHeaderView = controller.searchBar

        return controller
    })()

Controller:

在此输入图像描述

When I click on searchbar:

在此输入图像描述

You can place the UISearchBar of UISearchController in the navigation bar instead of table header

self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.searchBar.searchBarStyle = UISearchBarStyleMinimal;

// Include the search bar within the navigation bar.
self.navigationItem.titleView = self.searchController.searchBar;

self.definesPresentationContext = YES;

Swift version:

self.searchController.hidesNavigationBarDuringPresentation = false
self.searchController.searchBar.searchBarStyle = UISearchBarStyle.Minimal

// Include the search bar within the navigation bar.
self.navigationItem.titleView = self.searchController.searchBar
self.definesPresentationContext = true

It has to do with the navigation bar.

func willPresentSearchController(searchController: UISearchController) {
    self.navigationController?.navigationBar.translucent = true
}

func willDismissSearchController(searchController: UISearchController) {
    self.navigationController?.navigationBar.translucent = false
}

If you do this, then you will be able to add it to the table view header AND get the baked-in search controller animation!

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