搜索栏和分段控件

时间:2017-03-31 08:40:51

标签: ios swift xcode swift3

我在故事板上添加UISegmentedControl并在代码中添加SearchBar:

searchController = UISearchController(searchResultsController: nil)
searchController.searchBar.delegate = self
searchController.searchBar.sizeToFit()
searchController.dimsBackgroundDuringPresentation = false
discountTableView.tableHeaderView = searchController.searchBar

激活后搜索栏的位置无效。 如何解决? Before clickingAfter clicking

1 个答案:

答案 0 :(得分:0)

我通过实现UISearchController

的委托方法克服了这个问题
- (void)willPresentSearchController:(UISearchController *)searchController {

    if (searchController.searchBar.scopeButtonTitles.count == 0) {
        //somewhere I got this
        // there is an Apple bug when first time configuring search bar scope buttons
        // will be displayed no matter what with minimal searchbar
        // to fix this adding scope buttons right before user activates search bar
        searchController.searchBar.showsScopeBar = NO;
        searchController.searchBar.scopeButtonTitles = @[@"title 1", @"title 2"];
    }

}

希望它也能帮到你。