从iOS7开始,在我的UITableView顶部有额外的空间它有一个UITableViewStyleGrouped样式。
这里有一个例子:
tableview从第一个箭头开始,有35个像素的无法解释的填充,然后绿色的头是一个由viewForHeaderInSection返回的UIView(其中section为0)。
有人能解释一下这个35像素的数量是从哪里来的吗?我如何才能在不切换到UITableViewStylePlain的情况下摆脱它?
更新(回答):
在iOS 11及更高版本中:
tableView.contentInsetAdjustmentBehavior = .never
自我。automcallyadjustsscrollviewinsets = NO;
这在iOS 11中已弃用,所以你应该使用new属性
contentInsetAdjustmentBehavior在你的代码中,它应该修复这个问题。
if #available(iOS 11.0, *) {
collectionView.contentInsetAdjustmentBehavior = .never
}
在UIScrollView中添加了一个名为contentInsetAdjustmentBehavior的新属性,用于确定调整内容偏移量
当你使用UITableView.Style.grouped或UITableView.Style.InsetGrouped时,如果没有“tableview header”,tableview将自动添加一个顶部inset填充到“section header”,修复很简单:
self.tableview.tableFooterView = UIView(frame: CGRect.init(origin: .zero, size: CGSize.init(width: tableview.frame.size.width, height: 1)))
self.tableview.tableHeaderView = UIView(frame: CGRect.init(origin: .zero, size: CGSize.init(width: tableview.frame.size.width, height: 1)))
如果你使用uitableview。style。plain
self.tableview.contentInsetAdjustmentBehavior = .never
一般来说就足够了
我有一个VC与容器在它(btw是一个导航控制器的一部分),并嵌入其中是一个TableVC。
选择我的TableVC,查看属性面板:
取消勾选自动调整滚动插入
取消检查顶部栏下的延伸边
然后我设置了容器相对于父视图的自动约束:
容器的观点。top = ParentView。顶边
这为我完成了以下任务:
保持桌面拉到顶部,但就在我的导航栏下面
刷新后,表返回到这个位置,而不是像以前那样在导航栏下向上+
在实际的底部滚动表底部后,不缺它
我用这个设置进行了大量的试验和错误,但我最终发现了lekksi @移除UITableView单元格前的空白空间发布的这些小细节
你可以检测你的应用是否运行iOS7或更高版本,并在你的表视图委托中添加这两个方法(通常在你的UIViewController代码中)
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return CGFLOAT_MIN;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return CGFLOAT_MIN;
}
这可能不是一个优雅的解决方案,但对我来说是可行的
斯威夫特版本:
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return CGFloat.leastNormalMagnitude
}
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return CGFloat.leastNormalMagnitude
}