本文介绍了如何使用代码将 UISearchController Searchbar 放到导航栏上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

If I embed the ViewController into a Navigation Bar, navigationItem.titleView.resultSearchController?.searchBar will put a search bar into the navigation bar. However, I've created a UISearchController and a UINavigationBar with code. This time, the navBar is showing up, but the searchBar isn't.

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

navBar.topItem = resultSearchController?.searchBar doesn't work because topItem is a String value and resultSearchController?.searchBar is a UIView type. How can I achieve the same effect?

推荐答案

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

Create a UINavigationItem instance and add it to the created navigation bar.Add the search controller search bar to the UINavigationItem as 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
    }

这篇关于如何使用代码将 UISearchController Searchbar 放到导航栏上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 14:52