如何将segmentedControl和UISearchController作为UITableViewController头? (编程)

时间:2017-05-19 08:21:45

标签: ios swift xcode uitableview uisearchbar

我创建了一个UISearchController:let mySearchController = UISearchController(searchResultsController: nil)和一个分段控件:

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

    return segmentedControl
}

我怎么能将这些作为我的UITableViewController标题?我尝试将它们都放入UIView中,并使UIView成为标题,但是这样做时搜索栏不起作用。

应该怎么做?

2 个答案:

答案 0 :(得分:0)

您可以使用searchBar的范围栏属性添加范围按钮。

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

通过故事板 enter image description here

答案 1 :(得分:0)

使用scopeButtonTitles属性:

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

有关详细信息,请查看this question

更新:添加了对scopeButton更改的响应:

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!")
}