簡體   English   中英

iOS 15 中 UITableViewSections 之間的額外空間

[英]Extra Space Between UITableViewSections in iOS 15

UITableView包含多個沒有節頁腳的節,顯示節與節之間有額外的空間。 Xcode 的視圖調試器顯示它不是視圖,而只是一個空白區域。

在我的例子中,這種行為是不需要的。

嘗試添加 1.0/0.0 高度的頁腳無濟於事。 更改表視圖的style也沒有。

這是一個示例代碼:

import UIKit
 
final class ViewController: UITableViewController {
 
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.separatorColor = .yellow
    }
 
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 3
    }
 
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header = UIView()
        header.backgroundColor = .green
 
        return header
    }
 
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 20.0
    }
 
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }
 
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        cell.backgroundColor = .blue
 
        return cell
    }
 
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 30.0
    }
 
    override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let footer = UIView()
        footer.backgroundColor = .red
 
        return footer
    }
 
    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 20.0
    }
 
}

這是 iOS 14 和 iOS 15 中的 output:

iOS 14 iOS 15

在 iOS 15 添加了屬性sectionHeaderTopPadding 它會影響那個確切的空間。 該屬性的默認值為automaticDimension 將其設置為 0.0 可解決問題。

由於該屬性僅在 iOS 15 可用,您可能希望用可用性塊包裝它:

if #available(iOS 15.0, *) {
  tableView.sectionHeaderTopPadding = 0.0
}

這是問題的原始代碼片段,包括必要的更改:

import UIKit
 
final class ViewController: UITableViewController {
 
    override func viewDidLoad() {
        super.viewDidLoad()
 
        tableView.separatorColor = .yellow
        if #available(iOS 15.0, *) {
            tableView.sectionHeaderTopPadding = 0.0
        }
    }
 
    // The rest is without changes.
 
}

這是更改后的 iOS 15 中的 output:

iOS 15 更新后

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM