与搜索栏的交互不起作用

时间:2018-05-28 00:30:38

标签: ios swift

我按代码包含搜索栏,并使用下面的代码启用交互,显示搜索栏但是当我点击字段编辑时未启用:

let searchTableViewController = storyboard?.instantiateViewController(withIdentifier: "SearchBarTableVC") as? SearchBarTableVC
    searchTableViewController?.handleMapSearchDelegate = self
    searchTableViewController?.mapView = self.mapView

    searchBarController = UISearchController(searchResultsController: searchTableVC)
    searchBarController?.searchResultsUpdater = searchTableVC

    let searchBar = searchBarController?.searchBar

    searchBar?.sizeToFit()
    searchBar?.placeholder = "Digite o local"
    searchBar?.tintColor = UIColor.red
    searchBar!.isUserInteractionEnabled = true

    searchBar?.backgroundColor = UIColor(named: "ColorTransparent")

    navigationItem.titleView = searchBarController?.searchBar

    searchBarController?.hidesNavigationBarDuringPresentation = false
    searchBarController?.dimsBackgroundDuringPresentation = true

    definesPresentationContext = true

1 个答案:

答案 0 :(得分:0)

如果你使用的是iOS 11,首选的方法就是写:

navigationItem.searchController = searchController

这就是全部。您无需定位搜索栏或将其放在任何视图中。