无法以编程方式在我的导航栏中获取 UISearchController
Posted
技术标签:
【中文标题】无法以编程方式在我的导航栏中获取 UISearchController【英文标题】:Unable to get UISearchController in my navigation bar programmatically 【发布时间】:2019-01-17 12:19:16 【问题描述】:我有一个主视图控制器,它有一个全屏 UITableView
,由 Firebase 中的值和下面的 TabBarController 填充。 UITableView
的所有代码都以编程方式处理。我需要添加两个选项:首先,一个用于查询结果的搜索栏和一个用于从 Firebase 获取的各种类别的过滤器选项。
这是我从ViewController
更新的代码:
import UIKit
import Firebase
class PostTable: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchControllerDelegate,UISearchResultsUpdating
func updateSearchResults(for searchController: UISearchController)
var tableView:UITableView!
var posts = [Post]()
var searchController : UISearchController!
override func viewDidLoad()
super.viewDidLoad()
if Auth.auth().currentUser == nil
switchStoryboard()
tableView = UITableView(frame: view.bounds, style: .plain)
view.addSubview(tableView)
let cellNib = UINib(nibName: "PostTableViewCell", bundle: nil)
tableView.register(cellNib, forCellReuseIdentifier: "postCell")
var layoutGuide:UILayoutGuide!
layoutGuide = view.safeAreaLayoutGuide
tableView.leadingAnchor.constraint(equalTo: layoutGuide.leadingAnchor).isActive = true
tableView.topAnchor.constraint(equalTo: layoutGuide.topAnchor).isActive = true
tableView.trailingAnchor.constraint(equalTo: layoutGuide.trailingAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: layoutGuide.bottomAnchor).isActive = true
tableView.delegate = self
tableView.dataSource = self
tableView.tableFooterView = UIView()
tableView.reloadData()
searchController = UISearchController(searchResultsController: nil)
searchController.delegate = self
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false // displays tableview
let scb = self.searchController.searchBar
scb.tintColor = UIColor.white
scb.placeholder = "SEARCH"
scb.barTintColor = UIColor.white
if let textfield = scb.value(forKey: "searchField") as? UITextField
textfield.textColor = UIColor.green
if let backgroundview = textfield.subviews.first
backgroundview.backgroundColor = UIColor.white
backgroundview.layer.cornerRadius = 10
backgroundview.clipsToBounds = true
if #available(ios 11.0, *)
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
else
self.tableView.tableHeaderView = searchController.searchBar
searchController.searchBar.tintColor = UIColor.green
searchController.searchBar.barTintColor = UIColor.green
definesPresentationContext = true
observePosts()
【问题讨论】:
【参考方案1】:添加UISearchController
的变量
var searchController : UISearchController!
& 然后在viewDidLoad
中添加代码
searchController = UISearchController(searchResultsController: nil)
searchController.delegate = self
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false // displays tableview
let scb = self.searchController.searchBar
scb.tintColor = UIColor.white
scb.placeholder = "SEARCH"
scb.barTintColor = UIColor.white
if let textfield = scb.value(forKey: "searchField") as? UITextField
textfield.textColor = UIColor.greenColor
if let backgroundview = textfield.subviews.first
backgroundview.backgroundColor = UIColor.white
backgroundview.layer.cornerRadius = 10
backgroundview.clipsToBounds = true
if #available(iOS 11.0, *)
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
else
self.tableView.tableHeaderView = searchController.searchBar
searchController.searchBar.tintColor = UIColor.greenColor
searchController.searchBar.barTintColor = UIColor.greenColor
definesPresentationContext = true
【讨论】:
Cannot assign value of type 'PostTable' to type 'UISearchResultsUpdating?'
在第 3 行,当我将其转换为其他内容时它不起作用
你需要为UISearchControllerDelegate,UISearchResultsUpdating
添加代理
搜索栏不显示,整个导航栏为绿色。
你能分享输出的SS和你的相关代码吗? &您可以删除代码中更新的 navBar 的颜色
这是输出的screenshot,我会在一分钟内更新上面的代码【参考方案2】:
UITabbarController 没有 UINavigationController,因此它没有 UINavigationBar。
所以你可以这样做:
向您的 TableView 添加一个单元格以充当搜索栏 向 ViewController 添加导航栏 将自定义视图添加到您的视图中以充当容器可能还有更多选择,只是想为您指明正确的方向。
【讨论】:
【参考方案3】:override func viewDidLoad()
super.viewDidLoad()
searchController = UISearchController(searchResultsController: nil)
searchController.searchBar.autocapitalizationType = .none
if #available(iOS 11.0, *)
navigationItem.searchController = searchController
// Make the search bar always visible.
navigationItem.hidesSearchBarWhenScrolling = false
else
// For iOS 10 and earlier, place the search controller's search bar in the table view's header.
tableView.tableHeaderView = searchController.searchBar
searchController.delegate = self
searchController.dimsBackgroundDuringPresentation = false // The default is true.
【讨论】:
以上是关于无法以编程方式在我的导航栏中获取 UISearchController的主要内容,如果未能解决你的问题,请参考以下文章