0

I have created a UISearchController: let mySearchController = UISearchController(searchResultsController: nil) and a segmented control:

var mySegmentedControl: UISegmentedControl {
    let items = ["One", "Two", "Three"]
    let segmentedControl = UISegmentedControl(items: items)

    return segmentedControl
}

How would I have BOTH of these be my UITableViewController header? I tried putting them both into a UIView, and making the UIView the header, but the search bar was not functional when doing it this way.

How should this be done?

2 Answers2

0

You can add scope button using searchBar's scope bar property.

[yourSearchBar setShowsScopeBar:YES]; 
[yourSearchBar setScopeButtonTitles:@[@"button1",@"button2"]]; 

By storyboard enter image description here

KKRocks
  • 8,222
  • 1
  • 18
  • 84
0

Use the scopeButtonTitles property:

mySearchController.searchBar.scopeButtonTitles = ["One", "Two", "Three"]

Have a look at this question for more details.

UPDATE: Added response to scopeButton changes:

func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) { 
    // respond to your scopeBar selection here
    switch selectedScope {
    case 0:
        print("tab One selected")
        tableView.tintColor = UIColor.red
    case 1:
        print("tab two selected")
        tableView.tintColor = UIColor.blue
    case 2:
        print("tab Three selected")
        tableView.tintColor = UIColor.yellow
    default:
        print("Someone added a tab!")
}
Community
  • 1
  • 1
pesch
  • 1,976
  • 2
  • 17
  • 29
  • If I use these, will I be able to modify the color of the TableViewController depending on which scope is selected? – Marshall Scudder May 19 '17 at 08:54
  • I don't see why not. Try using `func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) { // respond to your scopeBar selection here }` – pesch May 19 '17 at 09:01
  • Here's a link to the docs:https://developer.apple.com/reference/uikit/uisearchbardelegate/1624280-searchbar You have to implement the `UISearchBarDelegate` – pesch May 19 '17 at 09:04
  • not sure if you meant `tintColor` or something else but you get the idea – pesch May 19 '17 at 09:22