You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS 11中UISearchController的UITableView与UISearchBar间出现异常空白

解决iOS 11下UISearchController的UITableView与UISearchBar间空白问题

嘿,我之前也碰到过iOS 11里这个烦人的空白问题!折腾了好一会儿才找到解决方案,给你分享下:

问题根源

iOS 11对滚动视图的内边距调整逻辑、以及UISearchController的布局展示机制做了改动,如果没有适配这些新特性,就容易出现searchBar和tableView之间的异常空白。

解决方案

根据你使用UISearchController的方式,对应调整配置:

情况1:将UISearchBar作为UITableView的tableHeaderView

在你的viewDidLoad里补充这些关键设置:

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 你的数组初始化&排序代码...
    self.cities = [@[@"Boston", @"New York", @"Oregon", @"Tampa", @"Los Angeles", @"Dallas", @"Miami", @"Olympia", @"Montgomery", @"Washington", @"Orlando", @"Detroit"] sortedArrayUsingComparator:^NSComparisonResult(NSString *obj1, NSString *obj2) {
        return [obj2 localizedCaseInsensitiveCompare:obj1] == NSOrderedAscending;
    }];
    self.results = [[NSMutableArray alloc] init];
    
    // 配置UISearchController
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchResultsUpdater = self;
    self.searchController.obscuresBackgroundDuringPresentation = NO; // 取消背景模糊,避免布局偏移
    self.searchController.searchBar.delegate = self;
    
    // 设置tableHeaderView
    self.tableView.tableHeaderView = self.searchController.searchBar;
    
    // iOS11+ 适配滚动视图内边距
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    // 可选:手动确保headerView高度匹配searchBar
    CGRect headerFrame = self.tableView.tableHeaderView.frame;
    headerFrame.size.height = self.searchController.searchBar.intrinsicContentSize.height;
    self.tableView.tableHeaderView.frame = headerFrame;
}

情况2:使用navigationItem.searchController(iOS11+推荐方式)

如果是把searchController直接绑定到导航栏,记得开启definesPresentationContext:

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 你的数组初始化&排序代码...
    
    // 配置UISearchController
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchResultsUpdater = self;
    self.definesPresentationContext = YES; // 确保搜索界面正确嵌入导航栏,避免布局错位
    
    // 绑定到导航栏
    self.navigationItem.searchController = self.searchController;
    
    // 适配滚动视图内边距
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
    } else {
        self.automaticallyAdjustsScrollViewInsets = YES;
    }
}

关键说明

  • obscuresBackgroundDuringPresentation设为NO:iOS11默认开启背景模糊,这个效果会干扰布局计算,导致空白出现;
  • contentInsetAdjustmentBehavior/automaticallyAdjustsScrollViewInsets:控制滚动视图是否根据安全区域自动调整内边距,适配iOS11前后的差异;
  • 手动设置tableHeaderView高度:有时候searchBar的intrinsicContentSize没有被正确识别,手动指定高度能解决高度不匹配导致的空白。

你可以先试试第一种情况的设置,应该就能消除那个烦人的空白啦!

内容的提问来源于stack exchange,提问作者Curt Rand

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:31:35