我的 Swift Xcode 專案中的 UICollectionView 有問題
我檢查了所有的插入和邊距,我嘗試了 ViewController 的:“調整滾動視圖插入”和許多其他東西,但他們都沒有以任何方式改變這一點。
我想減少/洗掉我的 collectionView 中第一行單元格上方的頂部間距
我創建了一個示例專案來查找此問題的解決方案/修復程式,但還沒有任何運氣
使用通過 Storyboard /Interface builder 插入的 Collection View 時,這似乎是一個問題,因為如果我在代碼端添加 collectionView,則不會發生這種情況
這是我的視圖控制器的代碼:
import UIKit
struct CVData: Hashable {
var name: String
var value: String
}
class ViewController: UIViewController {
@IBOutlet weak var myCollectionView: UICollectionView!
var dataSource: UICollectionViewDiffableDataSource<Section, CVData>!
var cvDataList: [CVData] = []
enum Section {
case main
}
var CVDataLabels: [String] = ["Label1:","Label2:","Label3:","Label4:","Label5:","Label6:","Label7:","Label8:"]
var CVDataValues: [String] = []
var snapshot: NSDiffableDataSourceSnapshot<Section, CVData>!
override func viewDidLoad() {
super.viewDidLoad()
configureCollectionView()
buildData()
// Do any additional setup after loading the view.
}
func configureCollectionView() {
let layoutConfig = UICollectionLayoutListConfiguration(appearance: .insetGrouped)
let listLayout = UICollectionViewCompositionalLayout.list(using: layoutConfig)
myCollectionView.collectionViewLayout = listLayout
myCollectionView.isScrollEnabled = false
let cellRegistration = UICollectionView.CellRegistration<UICollectionViewListCell, CVData> { (cell, indexPath, item) in
var content = UIListContentConfiguration.cell()
content.text = item.name
content.textProperties.font.withSize(8.0)
content.textProperties.font = UIFont.preferredFont(forTextStyle: .body)
content.secondaryText = item.value
content.prefersSideBySideTextAndSecondaryText = true
content.textProperties.adjustsFontSizeToFitWidth = false
cell.contentConfiguration = content
}
dataSource = UICollectionViewDiffableDataSource<Section, CVData>(collectionView: myCollectionView) {
(collectionView: UICollectionView, indexPath: IndexPath, identifier: CVData) -> UICollectionViewCell? in
// Dequeue reusable cell using cell registration (Reuse identifier no longer needed)
let cell = collectionView.dequeueConfiguredReusableCell(using: cellRegistration,
for: indexPath,
item: identifier)
// Configure cell appearance
cell.accessories = [.disclosureIndicator()]
return cell
}
}
func buildData() {
let cvDataList = [
CVData(name: self.CVDataLabels[0], value: "value1"),
CVData(name: self.CVDataLabels[1], value: "value2"),
CVData(name: self.CVDataLabels[2], value: "value3"),
CVData(name: self.CVDataLabels[3], value: "value4"),
CVData(name: self.CVDataLabels[4], value: "value5"),
CVData(name: self.CVDataLabels[5], value: "value6"),
CVData(name: self.CVDataLabels[6], value: "value6"), //added 20210510
CVData(name: self.CVDataLabels[7], value: "value7")
]
// Create a snapshot that define the current state of data source's data
self.snapshot = NSDiffableDataSourceSnapshot<Section, CVData>()
self.snapshot.appendSections([.main])
self.snapshot.appendItems(cvDataList, toSection: .main)
// Display data in the collection view by applying the snapshot to data source
self.dataSource.apply(self.snapshot, animatingDifferences: false)
}
}
這是 UICOllectionView 中的大小檢查器設定

任何幫助將不勝感激??
uj5u.com熱心網友回復:
我沒有使用過UICollectionViewCompositionalLayout.list,所以這只是來自一些快速的研究和實驗......
默認.headerMode的UICollectionLayoutListConfiguration是.none...當使用.insetGrouped的“額外空間”,我們看到的結果。
奇怪的是,還有一個headerTopPadding屬性,我們可能希望它在這里有所幫助。它的默認值是0...但如果有,似乎只適用IS頭視圖。
因此,如果我們為標題設定.headerMode = .supplementary并回傳一個空UICollectionReusableView值,我們就可以去掉多余的空間:

現在,頂部空間與默認的左/右填充相同。
這是我用于標題視圖的內容:
class EmptyHeaderView : UICollectionReusableView {
static let reuseIdentifier:String = "emptyHeaderView"
}
以及對您的configureCollectionView()func 的三個編輯:
func configureCollectionView() {
var layoutConfig = UICollectionLayoutListConfiguration(appearance: .insetGrouped)
// 1
// we're going to supply an "empty" header supplementary view
layoutConfig.headerMode = .supplementary
let listLayout = UICollectionViewCompositionalLayout.list(using: layoutConfig)
myCollectionView.collectionViewLayout = listLayout
myCollectionView.isScrollEnabled = false
let cellRegistration = UICollectionView.CellRegistration<UICollectionViewListCell, CVData> { (cell, indexPath, item) in
var content = UIListContentConfiguration.cell()
content.text = item.name
content.textProperties.font.withSize(8.0)
content.textProperties.font = UIFont.preferredFont(forTextStyle: .body)
content.secondaryText = item.value
content.prefersSideBySideTextAndSecondaryText = true
content.textProperties.adjustsFontSizeToFitWidth = false
cell.contentConfiguration = content
}
dataSource = UICollectionViewDiffableDataSource<Section, CVData>(collectionView: myCollectionView) {
(collectionView: UICollectionView, indexPath: IndexPath, identifier: CVData) -> UICollectionViewCell? in
// Dequeue reusable cell using cell registration (Reuse identifier no longer needed)
let cell = collectionView.dequeueConfiguredReusableCell(using: cellRegistration,
for: indexPath,
item: identifier)
// Configure cell appearance
cell.accessories = [.disclosureIndicator()]
return cell
}
// 2
// register a reusable supplementary view for the header
myCollectionView.register(EmptyHeaderView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: EmptyHeaderView.reuseIdentifier)
// 3
// provider for header suppleentary view
dataSource.supplementaryViewProvider = { (collectionView: UICollectionView, kind: String, indexPath: IndexPath) -> UICollectionReusableView? in
if kind == UICollectionView.elementKindSectionHeader {
if let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: EmptyHeaderView.reuseIdentifier, for: indexPath) as? EmptyHeaderView {
return headerView
}
}
fatalError("Something's wrong with the setup...")
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/381576.html
