0

I have a UISearchBar in my application. When I press a button to "open" the searchbar does a new view appear. But the problem is that the NavigationController changes and the UISearchBar disappear. How can I do so I can keep the current NavigationController with my searchbar even if a new view appear. (So I still searching when the new view appear)

P.s my code is not the best and I´m not using Storyboard!

class HomeController: UICollectionViewController, UICollectionViewDelegateFlowLayout, UISearchBarDelegate {

override func viewDidLoad() {
    super.viewDidLoad()

    collectionView?.backgroundColor = UIColor.white

    setupNavigationBar() 
}

Here is the new view that appear:

class UserSearchController: UICollectionViewController, UISearchBarDelegate {

override func viewDidLoad() {
    super.viewDidLoad()
    collectionView?.backgroundColor = UIColor.blue


    }

}

And here is the whole searchbar code:

import UIKit

var searchBar = UISearchBar()
var searchBarButtonItem: UIBarButtonItem?
var logoImageView: UIImageView!

extension HomeController {

    func setupNavigationBar() {
        let button = UIButton(type: .system)
        button.setImage(#imageLiteral(resourceName: "search"), for: .normal)
        button.addTarget(self, action: #selector(showSearchBar), for: .touchUpInside)
        button.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
        let barButton = UIBarButtonItem(customView: button)
        self.navigationItem.rightBarButtonItem = barButton

        let logoImage = UIImage(named: "home")!
        logoImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: logoImage.size.width, height: logoImage.size.height))
        logoImageView.image = logoImage
        navigationItem.titleView = logoImageView


        searchBar.delegate = self
        searchBar.searchBarStyle = UISearchBarStyle.minimal
        searchBar.placeholder = "Search"
        searchBar.barTintColor = UIColor.gray
        searchBarButtonItem = navigationItem.rightBarButtonItem

    }


    func showSearchBar() {
        let layout = UICollectionViewFlowLayout()
        let userSearchController = UserSearchController(collectionViewLayout: layout)
        self.navigationController?.pushViewController(userSearchController, animated: true)
        searchBar.alpha = 0
        navigationItem.titleView = searchBar
        navigationItem.setLeftBarButton(nil, animated: true)
        navigationItem.rightBarButtonItem = nil
        searchBar.showsCancelButton = true
        UIView.animate(withDuration: 0.5, animations: {
            self.searchBar.alpha = 1
        }, completion: { finished in
            self.searchBar.becomeFirstResponder()
        })
    }


    public func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        hideSearchBar()

    }

    func hideSearchBar() {
        navigationItem.setRightBarButton(searchBarButtonItem, animated: true)
        logoImageView.alpha = 0
        UIView.animate(withDuration: 0.3, animations: {
            self.navigationItem.titleView = self.logoImageView
            self.logoImageView.alpha = 1
        }, completion: { finished in

        })
    }


}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Stc
  • 25
  • 7

2 Answers2

1

The reason why your search bar is only visible on your first View Controller is because you are using the View Controller's titleView property. Each UIViewController has it's own titleView property, so if you push a View Controller onto your first VC, it will also need to have the titleView property set to a search bar view with the required configuration.

thexande
  • 1,645
  • 16
  • 23
  • Note that in iOS 11 this will change. – matt Aug 22 '17 at 23:37
  • Okay, thanks for the answer. But I´m not super good att Swift yet and I have tried search around a little bit. I still don´t really know how I should do it to fix my problem... Hope you could help me a little bit more. And thanks Matt for the info! – Stc Aug 24 '17 at 16:13
0

I think you can create a base class, add UISearchBar above the base class, and then you want the current controller with UISearchBar to inherit your base class

SnowStorm-L
  • 165
  • 11
  • Could you help me a little bit more with your answer? As I mentioned above I´m I not so good at Swift. Thanks! – Stc Aug 24 '17 at 16:14