【问题标题】:TableView ContentInset does not shrink cell width / Add horizontal padding to TableView contentsTableView ContentInset 不会缩小单元格宽度/向 TableView 内容添加水平填充
【发布时间】:2026-02-05 19:30:01
【问题描述】:

我想要一个全屏的UITableView。但是UITableView的内容应该左右有一个padding。

所以我尝试设置ContentInset。但现在单元格与UITableView 一样宽,UITableView 水平滚动。

有没有办法说UITableView 内容的宽度应该被水平内容插入变窄?还是我必须将填充添加到所有单元格和页眉/页脚视图?

我不想缩小表格视图本身,因为滚动指示器应该停留在屏幕的右侧而不是中间。 这里 (How to set the width of a cell in a UITableView in grouped style) 建议的解决方案似乎不像我想的那样通用,因为单元格和页眉和页脚视图必须了解填充(至少需要维护 3 个位置而不是一个)

【问题讨论】:

    标签: ios objective-c uitableview ios7 padding


    【解决方案1】:

    我不想缩小表格视图本身,因为滚动 指示器应位于屏幕右侧,而不是 中间。

    这让你开心吗?

    _tableView.clipsToBounds = NO;
    _tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 0, -30.f);
    

    如果您不喜欢clipsToBounds = NO 效果,可以将tableView 嵌入到容器视图中,即clipsToBounds = YES

    【讨论】:

    • 谢谢,我也测试了scrollIndicatorInsets,但忘记了clipsToBounds。现在看起来像请求了,但滚动行为很糟糕:D但这不是我的问题(还)。
    【解决方案2】:

    设置表格视图的布局边距。为此,请确保您在单元格中的约束是相对于超级视图边距设置的。

    tableView.layoutMargins = UIEdgeInsets(top: 0, left: 40, bottom: 0, right: 40)
    

    【讨论】: