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

iPhone X(iOS11.3)UITableView部分分隔线不显示问题求助

解决iOS 11.3 iPhone X上UITableView分隔线缺失的问题

哈哈,这个坑我适配iOS11的时候踩过!其实是iOS11之后UITableView的布局逻辑做了调整,尤其是layoutMargins和separatorInset的关联规则变了,再加上iPhone X的安全区域影响,就会出现分隔线显示不全的情况——而iOS10及以前的版本没有这套新逻辑,所以不会出问题。

核心原因

iOS11开始,UITableView默认会把separatorInset和自身的layoutMargins绑定,同时cell的layoutMargins会继承tableView的设置。这就导致分隔线不会完全贴合tableView的边缘,在你这种固定宽度的tableView上,就会看起来“缺失”一部分。

解决方法(亲测有效)

只需要在创建tableView时添加几个关键设置,同时在cell创建时同步调整cell的layoutMargins即可:

- (void)viewDidLoad {
    [super viewDidLoad];
    UITableView *tv = [[UITableView alloc] initWithFrame:CGRectMake(0, 44, 300, 500) style:UITableViewStylePlain];
    // 1. 强制分隔线的内边距为0
    tv.separatorInset = UIEdgeInsetsZero;
    // 2. 取消tableView的布局边距
    tv.layoutMargins = UIEdgeInsetsZero;
    // 别忘了设置代理和数据源(你之前的代码里可能漏了这部分)
    tv.delegate = self;
    tv.dataSource = self;
    [self.view addSubview:tv];
}

// 在cell创建的代理方法里,同步取消cell的布局边距
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"CellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        // 3. 确保cell的布局边距也为0,让分隔线完全贴合cell边缘
        cell.layoutMargins = UIEdgeInsetsZero;
    }
    cell.textLabel.text = [NSString stringWithFormat:@"第%ld行内容", indexPath.row];
    return cell;
}

补充说明

  • 如果你后续改用Auto Layout约束tableView,尽量让tableView的宽度约束到父视图的左右边缘,iOS11的安全区域逻辑会自动适配;但固定宽度的场景,上面的方法还是最直接的。
  • 这些设置对iOS10及以前的版本完全兼容,不会有副作用,可以放心在全版本中使用。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:16:43