【问题标题】:UISearchDisplayController table view overlapping search barUISearchDisplayController 表视图重叠搜索栏
【发布时间】:2014-05-25 15:45:53
【问题描述】:

我有一个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];
    }

}

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

搜索栏设置为UISearchBarStyleMinimal 并启用透明度。谁能告诉我如何阻止此内容与搜索栏重叠?理想情况下,内容会在搜索栏下消失,就好像它是视图的结尾一样。

【问题讨论】:

    标签: ios objective-c layout uisearchbar uisearchdisplaycontroller


    【解决方案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];
    
    }
    

    【讨论】:

      【解决方案2】:

      确保搜索栏未启用半透明(在 storyboard/xib 上)或通过代码启用。 然后将背景设置为白色或您想要的任何颜色。

      【讨论】:

        猜你喜欢
        • 2013-09-26
        • 1970-01-01
        • 1970-01-01
        • 2015-08-29
        • 1970-01-01
        • 1970-01-01
        • 2013-09-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多