如何将搜索栏与 Navigaitonbar 一起使用
How to use Search bar with Navigaitonbar
我已经成功地让 UISearchController 在一个新项目中工作,但是当我试图在我自己的项目中做同样的事情时,它的行为有所不同。
这就是我为让它工作所做的工作。
故事板:
这是我的视图控制器代码:
class ViewController: UIViewController, UISearchControllerDelegate, UISearchResultsUpdating, UISearchBarDelegate {
var searchController : UISearchController!
override func viewDidLoad() {
super.viewDidLoad()
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
let modalVC = mainStoryboard.instantiateViewController(withIdentifier: "SearchTableViewController_SBID")
self.searchController = UISearchController(searchResultsController: modalVC)
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) {
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我得到的结果是,当我点击搜索栏时,背景会变暗,当我开始输入时,table视图控制器会可见,搜索栏是否仍然存在在 table 视图控制器的导航栏中可见。
但是,当我尝试在我的项目中实现相同的功能时,当我开始在搜索栏上输入内容时,table 视图控制器将填满整个屏幕,并且导航栏不可见。
这是我项目的故事板:
请注意,这两个项目很相似,除了其中一个是 Navigation Controller -> Tab Bar View Controller
,当我在搜索栏上键入内容时,它不显示导航栏。
如何让导航栏显示在项目中 Navigation Controller -> Tab Bar View Controller
?
这是 2 个项目的样子:
故事板设置
两个视图控制器的编码
class ViewController1: UIViewController,UISearchBarDelegate {
@IBOutlet weak var lbl1: UILabel!
var searchBar:UISearchBar? = nil
override func viewDidLoad() {
super.viewDidLoad()
//Code to identify the searchbar
for view in (self.tabBarController?.navigationItem.titleView?.subviews)!
{
if view.isKind(of: UISearchBar.self)
{
searchBar = (view as! UISearchBar);
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//Assign Delegate and blank the search bar text
searchBar?.text = ""
searchBar?.delegate = self
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
lbl1.text = "Tab 1 : \(searchText)"
}
}
class ViewController2: UIViewController,UISearchBarDelegate {
@IBOutlet weak var lbl1: UILabel!
var searchBar:UISearchBar? = nil
override func viewDidLoad() {
super.viewDidLoad()
//Code to identify the searchbar
for view in (self.tabBarController?.navigationItem.titleView?.subviews)!
{
if view.isKind(of: UISearchBar.self)
{
searchBar = (view as! UISearchBar);
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
searchBar?.text = ""
searchBar?.delegate = self
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
lbl1.text = "Tab 2 : \(searchText)"
}
}
结果
我已经成功地让 UISearchController 在一个新项目中工作,但是当我试图在我自己的项目中做同样的事情时,它的行为有所不同。
这就是我为让它工作所做的工作。
故事板:
这是我的视图控制器代码:
class ViewController: UIViewController, UISearchControllerDelegate, UISearchResultsUpdating, UISearchBarDelegate {
var searchController : UISearchController!
override func viewDidLoad() {
super.viewDidLoad()
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
let modalVC = mainStoryboard.instantiateViewController(withIdentifier: "SearchTableViewController_SBID")
self.searchController = UISearchController(searchResultsController: modalVC)
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) {
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我得到的结果是,当我点击搜索栏时,背景会变暗,当我开始输入时,table视图控制器会可见,搜索栏是否仍然存在在 table 视图控制器的导航栏中可见。
但是,当我尝试在我的项目中实现相同的功能时,当我开始在搜索栏上输入内容时,table 视图控制器将填满整个屏幕,并且导航栏不可见。
这是我项目的故事板:
请注意,这两个项目很相似,除了其中一个是 Navigation Controller -> Tab Bar View Controller
,当我在搜索栏上键入内容时,它不显示导航栏。
如何让导航栏显示在项目中 Navigation Controller -> Tab Bar View Controller
?
这是 2 个项目的样子:
故事板设置
class ViewController1: UIViewController,UISearchBarDelegate {
@IBOutlet weak var lbl1: UILabel!
var searchBar:UISearchBar? = nil
override func viewDidLoad() {
super.viewDidLoad()
//Code to identify the searchbar
for view in (self.tabBarController?.navigationItem.titleView?.subviews)!
{
if view.isKind(of: UISearchBar.self)
{
searchBar = (view as! UISearchBar);
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//Assign Delegate and blank the search bar text
searchBar?.text = ""
searchBar?.delegate = self
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
lbl1.text = "Tab 1 : \(searchText)"
}
}
class ViewController2: UIViewController,UISearchBarDelegate {
@IBOutlet weak var lbl1: UILabel!
var searchBar:UISearchBar? = nil
override func viewDidLoad() {
super.viewDidLoad()
//Code to identify the searchbar
for view in (self.tabBarController?.navigationItem.titleView?.subviews)!
{
if view.isKind(of: UISearchBar.self)
{
searchBar = (view as! UISearchBar);
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
searchBar?.text = ""
searchBar?.delegate = self
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
lbl1.text = "Tab 2 : \(searchText)"
}
}
结果