【问题标题】:How do I put UISearchController Searchbar onto navigation bar with code如何使用代码将 UISearchController Searchbar 放到导航栏上
【发布时间】: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 类型。怎样才能达到同样的效果?

【问题讨论】:

    标签: ios swift xcode


    【解决方案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 成为我用代码创建的导航栏的标题视图
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-23
      • 2017-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多