Ios 如何使用代码将UISearchController搜索栏放在导航栏上
如果将ViewController嵌入导航栏,Ios 如何使用代码将UISearchController搜索栏放在导航栏上,ios,swift,xcode,Ios,Swift,Xcode,如果将ViewController嵌入导航栏,navigationItem.titleView.resultSearchController?.searchBar将在导航栏中插入搜索栏。但是,我创建了一个UISearchController和一个带有代码的UINavigationBar。这一次,导航栏出现了,但搜索栏没有出现 resultSearchController = UISearchController(searchResultsController: locationSearchTab
navigationItem.titleView.resultSearchController?.searchBar
将在导航栏中插入搜索栏。但是,我创建了一个UISearchController和一个带有代码的UINavigationBar。这一次,导航栏出现了,但搜索栏没有出现
resultSearchController = UISearchController(searchResultsController: locationSearchTable)
resultSearchController?.searchResultsUpdater = locationSearchTable
let searchBar = resultSearchController!.searchBar
searchBar.sizeToFit()
searchBar.delegate = self
let navBar: UINavigationBar = UINavigationBar(frame: CGRect(x: 0, y:0, width: 375, height: 64))
self.view.addSubview(navBar)
//navBar.topItem = resultSearchController?.searchBar
self.navigationItem.titleView = resultSearchController?.searchBar
navBar.topItem=resultSearchController?.searchBar
不工作,因为topItem
是字符串值,而resultSearchController?.searchBar
是UIView
类型。如何实现相同的效果?创建一个UINavigationItem
实例,并将其添加到创建的导航栏中。
class UIViewController: UIViewController, UISearchControllerDelegate, UISearchResultsUpdating, UISearchBarDelegate {
var searchController : UISearchController!
override func viewDidLoad() {
super.viewDidLoad()
self.searchController = UISearchController(searchResultsController: nil)
self.searchController.searchResultsUpdater = self
self.searchController.delegate = self
self.searchController.searchBar.delegate = self
self.searchController.hidesNavigationBarDuringPresentation = false
self.searchController.dimsBackgroundDuringPresentation = true
self.navigationItem.titleView = searchController.searchBar
self.definesPresentationContext = true
}
func updateSearchResults(for searchController: UISearchController) {
}
}
将搜索控制器搜索栏作为标题视图添加到UINavigationItem
class SearchViewController: UIViewController, UISearchControllerDelegate, UISearchResultsUpdating, UISearchBarDelegate {
var searchController: UISearchController!
override func viewDidLoad() {
super.viewDidLoad()
self.addNavigationbar()
}
func addNavigationbar() {
let navBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 60))
self.view.addSubview(navBar)
let navigationItem = UINavigationItem(title: "")
self.searchController = searchControllerWith(searchResultsController: nil)
navigationItem.titleView = self.searchController.searchBar
navBar.setItems([navigationItem], animated: false)
self.definesPresentationContext = true
}
func searchControllerWith(searchResultsController: UIViewController?) -> UISearchController {
let searchController = UISearchController(searchResultsController: searchResultsController)
searchController.delegate = self
searchController.searchResultsUpdater = self
searchController.searchBar.delegate = self
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = true
return searchController
}
此代码中没有创建导航栏。假设您将VC嵌入导航栏中,此代码将正常工作。我的问题是如何使搜索栏成为我用代码创建的导航栏的标题视图