如何使用代码将 UISearchController Searchbar 放到导航栏上

Posted

技术标签:

【中文标题】如何使用代码将 UISearchController Searchbar 放到导航栏上【英文标题】:How do I put UISearchController Searchbar onto navigation bar with code 【发布时间】:2017-08-18 12:44:04 【问题描述】:

如果我将 ViewController 嵌入到导航栏中,navigationItem.titleView.resultSearchController?.searchBar 将在导航栏中放置一个搜索栏。但是,我用代码创建了一个 UISearchController 和一个 UINavigationBar。这一次,navBar 出现了,但 searchBar 没有出现。

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 类型。怎样才能达到同样的效果?

【问题讨论】:

【参考方案1】:

创建一个UINavigationItem 实例并将其添加到创建的导航栏。 将搜索控制器搜索栏添加到 UINavigationItem 作为titleView。

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
    

【讨论】:

【参考方案2】:
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) 

    



【讨论】:

此代码中没有创建导航栏。假设您将 VC 嵌入导航栏,则此代码有效。我的问题是如何使 searchBar 成为我用代码创建的导航栏的标题视图

以上是关于如何使用代码将 UISearchController Searchbar 放到导航栏上的主要内容,如果未能解决你的问题,请参考以下文章

如何仅使用货币代码将数字格式化为货币?

如何使用php将php代码写入文件

如何将代码更改为交换指针而不是使用“临时”指针?

如何将 PHP 代码块放入 PHPDoc DocBlock

您将如何使用组合 API 编写此代码?

我将如何在此代码中使用 Sevenzipsharp?