Q:

How to add a UISearchbar on every view

Or is the easy way to implement the search bar manually on all needed views the correct way?
I have to add a searchbar on nearly every view in my app (in addition to add a button on every views navigationbar). But I am not sure what is the best approach to achieve this target.

Should I subclass a navigationbar or the whole navigation controller?
Or is the easy way to implement the search bar manually on all needed views the correct way?

If I should subclass which class is the right one?
My Idea is to subclass the UINavigationController, add a UISearchBar and after the search results are fetched to open a UITableViewController with the search results.
This is my current approach (without implementing the searchbar delegate just to check if I am on a working solution)
​
import UIKit

class MyNavigationControllerViewController: UINavigationController {

    var searchController : UISearchController!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationBar.backgroundColor = UIColor.red
        self.createSearchBar()
    }

    func createSearchBar() {
        let searchBar = UISearchBar()
        searchBar.showsCancelButton = true
        searchBar.placeholder = "Search"
        // searchBar.delegate = self
        self.navigationItem.titleView = searchBar
    }
}

At least, the debugger enters MyNavigationController but neither the searchbar is visible nor the red navigationbar.

A:

I would suggest using a protocol with an extension that will provide the configuration of the navigation bar and item. Then you can extend any view controller to conform to it and use the protocol's default implementation. 
import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        configureNavigationBar()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

extension ViewController: SearchController { }

protocol SearchController: class { }
extension SearchController where Self: UIViewController {

    func configureNavigationBar() {
        navigationController?.navigationBar.backgroundColor = UIColor.red
        let search = UISearchBar()
        search.placeholder = "Search"
        search.showsCancelButton = true
        navigationItem.titleView = search
    }
}

