UISearchController不会进入导航栏

时间:2015-05-15 17:38:29

标签: swift ios8 uisearchcontroller

极度沮丧,我似乎无法弄清楚为什么我的UISearchController search bar不会进入navigation bar处于活动状态时UISearchController。我在这个问题上花了一个多星期的时间,我似乎无法得到它。此外,我希望我的搜索结果显示在同一视图中,即搜索位置所在的视图。我已将searchResultsController设为零。我是否仍需要创建UITableView对象来管理搜索结果?当我计划在同一个视图控制器中显示我的搜索结果时,我不知道为什么会有UITableView对象管理它们的原因?这是我的代码,如果你能够帮我解决这个问题,我将是最快乐的人。先感谢您!

import UIKit
import MapKit
import CoreLocation


class ViewController: UIViewController, MKMapViewDelegate,        CLLocationManagerDelegate, UISearchBarDelegate, UISearchControllerDelegate,   UITableViewDelegate, UISearchResultsUpdating, UITableViewDataSource,  UINavigationBarDelegate  {


// Map
@IBOutlet weak var mapView: MKMapView!
let locationManager = CLLocationManager()

// Search
var searchController = UISearchController()


// Data model for table view
var results = [MKMapItem]()
var filteredResults = [MKMapItem]()

@IBOutlet weak var navigationBar: UINavigationBar!

// Search results controller is the table view that will display the search results
var resultsTableController = UITableViewController(style: UITableViewStyle.Plain)


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    // Call to the setupMapView method
    self.setupMapView()

    // Call to the setupLocationManager method
    self.setupLocationManager()

    self.resultsTableController.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "CellID")

    self.setupSearchController()
    self.setupSearchBar()


}

// MARK: Initialization methods


func setupSearchController() {

    // UITableViewController's tableViews data source
    resultsTableController.tableView.dataSource = self

    // UITableView's delegate
    resultsTableController.tableView.delegate = self


    resultsTableController.automaticallyAdjustsScrollViewInsets = true

    // Initialize our search controller with the UITableViewController
    self.searchController = UISearchController(searchResultsController: nil)
    searchController.searchResultsUpdater = self

    // Search controller and search bar delegate
    self.searchController.delegate = self
    self.searchController.searchBar.delegate = self

    // Placeholder text for the search bar before it is clicked
    self.searchController.searchBar.placeholder = "Country, City, Address"

    // The navigation will be hidden when the search is started
    self.searchController.hidesNavigationBarDuringPresentation = false


    self.searchController.searchBar.sizeToFit()

    self.definesPresentationContext = true

    self.resultsTableController.tableView.tableHeaderView = self.searchController.searchBar


}

func setupSearchBar() {

    // Set search bar position and dimensions
    var searchBarFrame: CGRect = self.searchController.searchBar.frame
    var viewFrame = self.view.frame
    self.searchController.searchBar.frame = CGRectMake(searchBarFrame.origin.x, searchBarFrame.origin.y + 64,viewFrame.size.width, 44)


    // Add search controller's search bar to our view and bring it to forefront
    self.view.addSubview(self.searchController.searchBar)
    //self.view.bringSubviewToFront(self.searchController.searchBar)
}


// MARK: UISearchControllerDelegate

func willPresentSearchController(searchController: UISearchController) {



    // Set table view position and dimensions
    var tableViewFrame: CGRect = self.resultsTableController.tableView.frame
    var viewFrame = self.view.frame

    var searchTableViewRect = CGRectMake(tableViewFrame.origin.x, tableViewFrame.origin.y, viewFrame.width, viewFrame.height)
    self.resultsTableController.tableView.frameForAlignmentRect(searchTableViewRect)

    self.mapView.removeAnnotations(self.mapView.annotations)
}

func willDismissSearchController(searchController: UISearchController) {



}

0 个答案:

没有答案