iOS v10 或更低版本:如何以编程方式在导航栏底部添加 UISearchController
Posted
技术标签:
【中文标题】iOS v10 或更低版本:如何以编程方式在导航栏底部添加 UISearchController【英文标题】:iOS v10 or lower: How to add UISearchController in the bottom of navigation bar Programmatically 【发布时间】:2018-11-06 06:39:14 【问题描述】:我的应用的最低 ios 版本是 10.3。我可以在 iOS 11 的导航栏底部添加搜索栏。但是对于较低版本的 iOS 11,我想要相同的搜索栏,它应该是程序化的。请帮忙。谢谢
fileprivate func _setupNavigationBar()
let searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search Here"
if #available(iOS 11.0, *)
navigationItem.searchController = searchController
else
// TODO: ????????????????
【问题讨论】:
您可以将其放置为titleView
,或者创建自己的自定义导航栏
【参考方案1】:
class ViewController: UIViewController, UISearchControllerDelegate
var controladorDeBusca: UISearchController!
var resultsTableViewController: ResultsTableViewController?
override func viewDidLoad()
super.viewDidLoad()
resultsTableViewController = storyboard!.instantiateViewController(withIdentifier: "resultsTableViewController") as? ResultsTableViewController
setUpSearchAtNaviagtion()
func setUpSearchAtNaviagtion()
controladorDeBusca = UISearchController(searchResultsController: resultsTableViewController)
controladorDeBusca.delegate = self
controladorDeBusca.searchResultsUpdater = resultsTableViewController
controladorDeBusca.dimsBackgroundDuringPresentation = true
definesPresentationContext = true
controladorDeBusca.loadViewIfNeeded()
//Configura a barra do Controlador de busca
controladorDeBusca.searchBar.delegate = resultsTableViewController
controladorDeBusca.hidesNavigationBarDuringPresentation = false
controladorDeBusca.searchBar.placeholder = "Search place"
controladorDeBusca.searchBar.sizeToFit()
controladorDeBusca.searchBar.barTintColor = navigationController?.navigationBar.barTintColor
controladorDeBusca.searchBar.tintColor = self.view.tintColor
//Adiciona a barra do Controlador de Busca a barra do navegador
navigationItem.titleView = controladorDeBusca.searchBar
试试这个..
【讨论】:
我不想通过替换导航栏的标题视图来设置搜索栏。我只想将它添加到导航栏下方。如果我点击座位栏进行编辑,导航栏会被滚动到顶部隐藏。以上是关于iOS v10 或更低版本:如何以编程方式在导航栏底部添加 UISearchController的主要内容,如果未能解决你的问题,请参考以下文章
如何在 iOS 10 或更低版本中使用 webview? [复制]