[英]How to navigate to another view controller when search bar is clicked
I am a bit stuck here. 我有点卡在这里。 I have a search bar embedded in my navigation bar as the title which I want.
我的导航栏中嵌入了一个搜索栏,作为我想要的标题。
However next I would like to navigate to another view controller containing a UIsearchcontroller in a collection view, and when cancel is tapped the user is brought back to the previous view. 但是,接下来,我想导航到另一个在集合视图中包含UIsearchcontroller的视图控制器,并在单击“取消”时将用户带回到上一个视图。 (Very similar to Facebook or Instagrams use of the search bar controller)
(非常类似于Facebook或Instagram对搜索栏控制器的使用)
Is there anyway I can use the search bar as a button to navigate to the next view controller that handles all of the search functions? 无论如何,我可以使用搜索栏作为按钮来导航到处理所有搜索功能的下一个视图控制器吗?
This is what I have so far for the search bar. 到目前为止,这是我对搜索栏的了解。
func setUpNavBar() {
let searchBar = UISearchBar()
searchBar.sizeToFit()
searchBar.searchBarStyle = .minimal
searchBar.placeholder = "Search by username"
searchBar.tintColor = UIColor.lightGray
searchBar.barTintColor = UIColor.lightGray
navigationItem.titleView = searchBar
searchBar.isTranslucent = true
}
Edit 编辑
Just to add on to the answer below, if anyone comes across this and you want to get the effect that treats the search bar as a button use 只是添加到下面的答案中,如果有人遇到过这种情况,并且您希望获得将搜索栏视为按钮使用的效果
func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
self.present(UINavigationController(rootViewController: SearchBarController()), animated: true, completion: nil)
searchBar.setShowsCancelButton(false, animated: true)
return false
}
This way, when you click the search bar, you do not get the effect that shows the search bar active,( if that makes sense). 这样,当您单击搜索栏时,您不会获得使搜索栏处于活动状态的效果(如果有意义)。 When you tap the search bar, there is no effect, it just takes you to the (real) search view controller then the search bar is active and the keyboard is displayed with a cancel button.
当您点击搜索栏时,没有任何效果,只是将您带到(实际的)搜索视图控制器,然后搜索栏处于活动状态,并且显示带有取消按钮的键盘。 I hope that makes sense, if anyone wants an example go to Instagram, FB, or Pinterest and tap on their search bar or search icon and you'll see.
我希望这是有道理的,如果有人希望举个例子,请转到Instagram,FB或Pinterest,然后点击其搜索栏或搜索图标,您会看到。
Add UISearchBarDelegate
in first view controller. 在第一个视图控制器中添加
UISearchBarDelegate
。 And in searchBarTextDidBeginEditing
method present the second view controller. 在
searchBarTextDidBeginEditing
方法中,提供第二个视图控制器。
//ViewController.swift
class ViewController: UIViewController, UISearchBarDelegate {
override func viewDidLoad() {
super.viewDidLoad()
setUpNavBar()
}
func setUpNavBar() {
let searchBar = UISearchBar()
searchBar.delegate = self
searchBar.sizeToFit()
searchBar.searchBarStyle = .minimal
searchBar.placeholder = "Search by username"
searchBar.tintColor = UIColor.lightGray
searchBar.barTintColor = UIColor.lightGray
navigationItem.titleView = searchBar
searchBar.isTranslucent = true
}
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
self.present(UINavigationController(rootViewController: SearchViewController()), animated: false, completion: nil)
}
}
//SearchViewController.swift
class SearchViewController: UIViewController {
let searchBar = UISearchBar()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
setUpNavBar()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
searchBar.becomeFirstResponder()
}
func setUpNavBar() {
searchBar.sizeToFit()
searchBar.searchBarStyle = .minimal
searchBar.placeholder = "Search by "
searchBar.tintColor = UIColor.lightGray
searchBar.barTintColor = UIColor.lightGray
navigationItem.titleView = searchBar
searchBar.isTranslucent = true
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.