Keep your code clean is very important, it enables you to reuse code. To achieve this you could use some common design patterns like Strategy and you could use protocol-oriented programming.
I will leave some sources so you can go deeper into how to achieve this by using SOLID principles: https://medium.com/ios-expert-series-or-interview-series/solid-design-principle-using-swift-34bb1731cfb3
What you need in this case is a simple abstraction, you need to create a function that is able to configure any SearchController. I will give you an example, but don't let this example to limit you, there are many other (and better ways) to achieve this.
class SearchBarManager: UIViewController {
private func configureSearchController(searchController: UISearchController, navigationItem: UINavigationItem, title: String, scopeButtonTitles: [String] = [], selectedScopeButtonIndex: Int = 0) {
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = title
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
definesPresentationContext = true
searchController.searchBar.scopeButtonTitles = scopeButtonTitles
searchController.searchBar.selectedScopeButtonIndex = selectedScopeButtonIndex
}
func configureAlbumSearchController(_ searchController: UISearchController, _ navigationItem: UINavigationItem) {
configureSearchController(searchController: searchController, navigationItem: navigationItem, title: "Search Albums")
}
func configurePhotoSearchController(_ searchController: UISearchController, _ navigationItem: UINavigationItem) {
configureSearchController(searchController: searchController, navigationItem: navigationItem, title: "Search Photos", scopeButtonTitles: ["1 Column", "2 Columns", "3 Columns"], selectedScopeButtonIndex: 2)
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…