Reputation: 113
I was wondering if it is possible to add a UISegmentedControl
with UISearchBar
inside the same navigation bar?
I tried to minimize my UISearchBar
, but it didn't work:
lazy var searchBar = UISearchBar(frame: CGRect.zero)
override func viewDidLoad() {
tableView.delegate = self
tableView.dataSource = self
searchBar.searchBarStyle = .minimal
navigationItem.titleView = searchBar
}
I know apps like Apple Music have done this:
Whether the segment is part of the whole UISearchBar could also be a question.
Upvotes: 2
Views: 2780
Reputation: 12198
Try this
let searchBar = UISearchBar()
searchBar.placeholder = "Biblioteket ditt"
searchBar.showsScopeBar = true
searchBar.barTintColor = UIColor(white: 0.9, alpha: 0.1)
searchBar.scopeButtonTitles = ["Apple Music", "Biblioteket ditt"]
// To change UISegmentedControl color only when appeared in UISearchBar
UISegmentedControl.appearance(whenContainedInInstancesOf: [UISearchBar.self]).tintColor = .red
self.navigationItem.titleView = searchBar
Here is the screenshot of above code:
Upvotes: 4