搜索栏未显示在视图控制器中

Search bar not showing in view controller

我正在尝试以编程方式将搜索栏添加到我的视图控制器,但是问题

我遇到的是搜索栏在视图控制器中不可见,我想知道如何在视图控制器中显示搜索栏。

class LocationViewController: UIViewController {

    let locationManager = CLLocationManager()
    var resultSearchControlle: UISearchController? = nil

    @IBOutlet weak var mapView: MKMapView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let locationSearchTable = storyboard!.instantiateViewController(identifier: "LocationSearchTable") as! LocationTableViewController

        resultSearchControlle = UISearchController(searchResultsController: locationSearchTable)

        resultSearchControlle?.searchResultsUpdater = locationSearchTable

        let searchBar = resultSearchControlle!.searchBar

        searchBar.sizeToFit()

        searchBar.placeholder = "Search for a place"

        navigationItem.titleView = resultSearchControlle?.searchBar
        navigationItem.hidesSearchBarWhenScrolling = false

        definesPresentationContext = true

        locationSearchTable.mapView = mapView

    }

我正在使用您的代码并得到这个结果

var resultSearchControlle: UISearchController? = nil
    // MARK: - Constants

    //static let reuseIdentifier = "Cell"
    override func viewDidLoad() {
        resultSearchControlle = UISearchController()
        let searchBar = resultSearchControlle!.searchBar
        searchBar.sizeToFit()
        searchBar.placeholder = "Search for a place"
        navigationItem.titleView = resultSearchControlle?.searchBar
        navigationItem.hidesSearchBarWhenScrolling = false
        definesPresentationContext = true

      //  locationSearchTable.mapView = mapView

    } 

在导航控制器中嵌入您希望搜索栏出现的视图控制器:

Select Storyboard 中的 LocationViewController,然后在菜单中..

编辑器 -> 嵌入 -> 导航控制器