无法实现UISearchController

时间:2015-06-16 14:21:20

标签: objective-c uitableview uiviewcontroller ios8 uisearchcontroller

我无法实施UISearchController

我有UIViewController,其中包含UITableView以及表视图下方的几个按钮。

- (void)viewDidLoad {

    [super viewDidLoad];
    ...
    _searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    _searchController.searchResultsUpdater = self;
    _searchController.dimsBackgroundDuringPresentation = NO;
    _searchController.searchBar.scopeButtonTitles = @[];
    _searchController.searchBar.delegate = self;
    NSLog(@"tableViewHeader %@", NSStringFromCGRect(_tableView.tableHeaderView.frame));
    NSLog(@"searchBar       %@", NSStringFromCGRect(_searchController.searchBar.frame));
    _tableView.tableHeaderView = _searchController.searchBar;
    ...
}

NSLog返回:

tableViewHeader {{0, 0}, {0, 0}}
searchBar       {{0, 0}, {320, 44}}

一般来说,我似乎无法看到tableViewHeader。由于我没有使用Storyboard,因此我将UITableView实例化如下:

_tableView = [[UITableView alloc] init];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.separatorInset = UIEdgeInsetsZero;
_tableView.layoutMargins = UIEdgeInsetsZero;
_tableView.backgroundColor = [UIElementsStyleKit tableCellBackground];

[self.view addSubview:_tableView];

可能出现什么问题?

由于

//编辑: 如果它不是隐含的,UITableView的维度由NSLayoutConstraints定义,并且它显示完美。问题只是UISearchBar没有显示。

1 个答案:

答案 0 :(得分:0)

我的问题的解决方案是,在将UISearchController作为子视图添加到UITableView之后,我定义了self.view

一旦我在[self.view addSubview:_tableView]之前移动代码,一切都按预期工作。

相关问题