Table 视图位置不会夹在导航栏上

Table view position doesn't clip to navigation bar

当我使用自定义 table 视图创建 table 视图时,导航栏和 Table 视图顶部之间有空白 space。

看起来:

我已经添加了与 view.topAnchor 相等的约束。 我不知道为什么会这样。

如何将 Table 视图裁剪到顶部?

提前致谢。

确保您没有添加任何 header。 如果您使用的是 nib/storyboard 检查 ViewController 设计 -> Select tableview -> 将 Footer height & Header height 设置为 0.

如果您使用的是代码,请查看此示例,了解如何使用代码删除 header - How to hide first section header in UITableView (grouped style)

默认情况下需要 28,这就是它在顶部显示空白的原因。

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 0
}

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 0
}

设置上面的代码并确保委托方法将被调用。

试试这个技巧,在 viewDidLoad:

中添加一个空白的 UIView...
view.addSubview(UIView())

我忽略了原因,但这会破坏 UIScrollView 和导航栏之间的 link...这可能会解决您的问题。