简体   繁体   English

iOS 15:删除 UITableView 中单元格之前的空白区域

[英]iOS 15: Remove empty space before cells in UITableView

I am using UITableView for a multi-section list.我将UITableView用于多节列表。 The issue I am seeing is a space above the cells of each section, even if I set tableView(_:heightForHeaderInSection:) to be 0. This occurs even when there is only one section and I set tableView(_:viewForHeaderInSection:) to be nil .我看到的问题是每个部分的单元格上方有一个空格,即使我将tableView(_:heightForHeaderInSection:)设置为 0。即使只有一个部分并且我将tableView(_:viewForHeaderInSection:)nil

I have tried all other answers on StackOverflow relating to inset overrides/edge expanding but none have worked.我已经尝试了 StackOverflow 上与插入覆盖/边缘扩展相关的所有其他答案,但没有一个有效。

Example:例子:

iOS 表视图的屏幕截图。每个部分标题上方是一个与表格背景颜色相同的间隙。

Check if you are only seeing this issue on iOS 15. If so, this may be caused by the newly introduced UITableView.sectionHeaderTopPadding property.检查您是否只在 iOS 15 上看到此问题。如果是,这可能是由新引入的UITableView.sectionHeaderTopPadding属性引起的。 You will need to set this value to 0 in order to remove the spacing before section headings:您需要将此值设置为0以删除节标题之前的间距:

let tableView = UITableView()
tableView.sectionHeaderTopPadding = 0

// Etc.

This property is only available in iOS 15 so you will need an API check if building for earlier versions.此属性仅在 iOS 15 中可用,因此如果为早期版本构建,您将需要进行 API 检查。

If you're not on iOS 15, this question has most of the answers to this issue.如果您使用的不是 iOS 15,则此问题包含此问题的大部分答案。

Another approach is to set the sectionHeaderTopPadding value found in UITableView's UIAppearance proxy, which ensures that the value will be applied to every instance of UITableView in your application:另一种方法是设置在 UITableView 的UIAppearance代理中找到的sectionHeaderTopPadding值,这确保该值将应用于应用程序中 UITableView 的每个实例:

if #available(iOS 15.0, *) {
    UITableView.appearance().sectionHeaderTopPadding = CGFloat(0)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM