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

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

    return segmentedControl
}


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

应该怎么做?

最佳答案

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

关于ios - 如何将segmentedControl和UISearchController作为UITableViewController header ? (以编程方式),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44064937/

10-13 02:23