UISearchDisplayController table view overlapping s

2019-05-31 02:06发布

I have a UITableViewController subclass, displayed in a modal view on an iPad. The view controller has a UISearchDisplayController subclass with a UISearchBar included in the header view of the table.

The subclassed UISearchDisplayController is called NoAnimationSearchDisplayController and I have overridden the - (void)setActive:(BOOL)visible animated:(BOOL)animated method to prevent the search bar from animating into the navigation bar when it's set to active. The method override is below...

- (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];
    }

}

The problem I'm having, is that when I have searched, and results are displayed in my search display controller's table view, everything looks fine untill i try to scroll down the list, at this point the content within the cells appears above the search bar, as shown in the following screen:

enter image description here

The search bar is set to UISearchBarStyleMinimal and transparency is enabled. Can anyone let me know how to stop this content overlapping the search bar? Ideally the content would disappear under the search bar as if it was the end of the view.

2条回答
放我归山
2楼-- · 2019-05-31 02:45

Be sure the search bar is not translucent enabled ( on storyboard/xib ) or by code. Then make the background to white or whatever color you want.

查看更多
够拽才男人
3楼-- · 2019-05-31 02:49

The answer was to manually change the frame of the table view provided by the UIsearchDisplayController in the appropriate delegate method...

- (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];

}
查看更多
登录 后发表回答