UISearchController in a UIViewController

前端 未结 4 1945
无人共我
无人共我 2020-12-28 10:13

I\'m looking to create similar functionality to Apple\'s maps application in Swift. Is there anyway to integrate a UISearchController in to a regular view (i.e.: not a UITab

4条回答
  •  悲&欢浪女
    2020-12-28 10:34

    Trying to figure out UISearchController myself. Setting it to the titleView is convenient, but on one of my pages, I had to put the searchBar near the top of the UIViewController:

    // Add a normal View into the Storyboard.
    // Set constraints:
    // - height: 44
    // - leading and trailing so that it spans the width of the page
    // - vertical position can be anywhere based on your requirements, like near the top
    @IBOutlet weak var searchContainerView: UIView!
    
    var searchResultsController = UISearchController()
    
    override func viewDidLoad() {
        // TODO: set the searchResultsController to something
        let controller = UISearchController(searchResultsController: nil)
        // have the search bar span the width of the screen
        controller.searchBar.sizeToFit()
        // add search bar to empty View
        searchContainerView.addSubview(controller.searchBar)
        searchResultsController = controller
    }
    

    UPDATE:

    After implementing UISearchController in a project or two, I found myself gravitating toward @adauguet's approach of embedding the search bar into the Navigation Bar.

    Here's the code in Swift. One difference though is that it doesn't set the searchBar delegate, since searchResultsUpdater already listens for text changes.

    override func viewDidLoad() {
        super.viewDidLoad()
    //        locationManager.delegate = self
    //        locationManager.desiredAccuracy = kCLLocationAccuracyBest
    //        locationManager.requestWhenInUseAuthorization()
    //        locationManager.requestLocation()
        let locationSearchTable = storyboard!.instantiateViewControllerWithIdentifier("LocationSearchTable") as! LocationSearchTable
        resultSearchController = UISearchController(searchResultsController: locationSearchTable)
        resultSearchController?.searchResultsUpdater = locationSearchTable
        let searchBar = resultSearchController!.searchBar
        searchBar.sizeToFit()
        searchBar.placeholder = "Search for places"
        navigationItem.titleView = resultSearchController?.searchBar
        resultSearchController?.hidesNavigationBarDuringPresentation = false
        resultSearchController?.dimsBackgroundDuringPresentation = true
        definesPresentationContext = true
    }
    

    Also, I wrote a blog post that creates a project from scratch that uses UISearchController to display map search results. It also does other things that you might want in a map project, like get the user location, drop pins, parse placemarks into a one-line address, and create callout buttons that take you to Apple Maps for driving directions.

    http://www.thorntech.com/2016/01/how-to-search-for-location-using-apples-mapkit/

    The blog post is quite long, so here's the associated git repo if you just want to skip to the code:

    https://github.com/ThornTechPublic/MapKitTutorial

提交回复
热议问题