
Я проверил все вставки и поля, попробовал ViewController: «Настроить вставки прокрутки» и многое другое, но ни один из них никак не изменил ситуацию.
i хочу уменьшить/удалить верхний интервал над первой строкой ячеек в моей коллекции.
Я создал образец проекта, чтобы найти решение/исправление этой проблемы, но пока не повезло.
Похоже, это проблема при использовании представления коллекции, вставленного с помощью раскадровки/построителя интерфейса, потому что этого не произойдет, если я добавлю CollectionView на стороне кода.
Вот код моего контроллера представления:
/>
Код: Выделить всё
import UIKit
struct CVData: Hashable {
var name: String
var value: String
}
class ViewController: UIViewController {
@IBOutlet weak var myCollectionView: UICollectionView!
var dataSource: UICollectionViewDiffableDataSource!
var cvDataList: [CVData] = []
enum Section {
case main
}
var CVDataLabels: [String] = ["Label1:","Label2:","Label3:","Label4:","Label5:","Label6:","Label7:","Label8:"]
var CVDataValues: [String] = []
var snapshot: NSDiffableDataSourceSnapshot!
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 { (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(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()
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)
}
}

Будем очень благодарны за любую помощь
Подробнее здесь: https://stackoverflow.com/questions/703 ... op-spacing
Мобильная версия