UISearchDisplayController表视图重叠搜索栏

时间:2014-04-11 13:03:54

标签: ios objective-c layout uisearchbar uisearchdisplaycontroller

我有一个UITableViewController子类,在iPad的模态视图中显示。视图控制器有一个UISearchDisplayController子类,表的标题视图中包含UISearchBar

子类UISearchDisplayController被称为NoAnimationSearchDisplayController,我已覆盖- (void)setActive:(BOOL)visible animated:(BOOL)animated 方法,以防止搜索栏在设置为活动时动画进入导航栏。方法覆盖低于......

- (void)setActive:(BOOL)visible animated:(BOOL)animated
{

    if (self.active == visible) {
        return;
    }

    [self.searchContentsController.navigationController setNavigationBarHidden:YES animated:NO];

    [super setActive:visible animated:animated];

    [self.searchContentsController.navigationController setNavigationBarHidden:NO animated:NO];

    if (visible) {
        [self.searchBar becomeFirstResponder];
    } else {
        [self.searchBar resignFirstResponder];
    }

}

我遇到的问题是,当我搜索过,结果显示在我的搜索显示控制器的表格视图中时,一切看起来都很好,直到我尝试向下滚动列表,此时单元格中的内容显示在搜索栏上方,如以下屏幕所示:

enter image description here

搜索栏设置为UISearchBarStyleMinimal并启用透明度。任何人都可以让我知道如何阻止这个内容重叠搜索栏?理想情况下,内容会在搜索栏下消失,就好像它是视图的结尾一样。

2 个答案:

答案 0 :(得分:1)

答案是在适当的委托方法中手动更改UIsearchDisplayController提供的表格视图的框架......

- (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView {

    /**
     *  Remove content inset automatically set by UISearchDisplayController as we are forcing the
     *  search bar to stay in the header view of the table, and not go into the navigation bar.
     */

    [tableView setContentInset:UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f)];


    /**
     *  Recalculate the bounds of our table view to account for the additional 44 pixels taken up by
     *  the search bar, but store this in an iVar to make sure we only adjust the frame once. If we 
     *  don't store it in an iVar it adjusts the frame for every search.
     */

    if (CGRectIsEmpty(_searchTableViewRect)) {

        CGRect tableViewFrame = tableView.frame;

        tableViewFrame.origin.y = tableViewFrame.origin.y + 44;
        tableViewFrame.size.height =  tableViewFrame.size.height - 44;

        _searchTableViewRect = tableViewFrame;

    }

    [tableView setFrame:_searchTableViewRect];

}

答案 1 :(得分:1)

确保搜索栏不是半透明的(在storyboard / xib上)或代码。 然后将背景变为白色或任何你想要的颜色。