简体   繁体   English

单击搜索栏时如何导航到另一个视图控制器

[英]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.

相关问题 单击搜索栏中的输入按钮后,如何更新视图控制器以显示搜索结果? - how to update the view controller to show the search results after clicked enter button in search bar? 如何在推送通知中导航到另一个视图控制器 - How to navigate to another view controller in push notifications 如何导航到标签栏控制器中嵌入的View Controller - How to navigate to a View Controller embedded in tab bar controller 如何从一个视图控制器导航到另一视图控制器? - How to navigate from one view controller to another view controller? 如何从popoverpresentation视图控制器导航到另一个视图控制器 - How to navigate from popoverpresentation view controller to another view controller 导航到第二个视图控制器时,标签栏隐藏 - Tab Bar hides when navigate to second view controller 导航离开标签栏视图控制器时如何删除标签栏? - How do remove tab bar when navigate away from tabbar view controller? Swift - 在 2 个视图控制器之间导航时如何删除黑色导航栏 - Swift - how to remove black navigation bar when navigate between 2 view controller 如何在 Swift 中以编程方式从一个控制器导航到另一个视图控制器? - How to navigate from one controller to another View Controller programmatically in Swift? 按下搜索栏时切换到另一个视图 controller 时意外发现 nil - unexpectedly found nil when switching to another view controller when search bar is pressed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM