Test tester
Test tester

Reputation: 113

How to implement segment control with search bar within navigation bar?

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:

enter image description here Whether the segment is part of the whole UISearchBar could also be a question.

Upvotes: 2

Views: 2780

Answers (1)

AamirR
AamirR

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:

enter image description here

Upvotes: 4

Related Questions