Clint
Clint

Reputation: 387

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. (Very similar to Facebook or Instagrams use of the search bar controller)

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.

Upvotes: 3

Views: 5000

Answers (1)

RajeshKumar R
RajeshKumar R

Reputation: 15748

Add UISearchBarDelegate in first view controller. And in searchBarTextDidBeginEditing method present the second view controller.

//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
    }
}

Upvotes: 6

Related Questions