有一个包含自动调整大小的单元格的集合,它还包含页眉和页脚。问题是页眉和页脚没有出现在它们的位置,但如果你滚动浏览集合,那么页眉和页脚将落在原位。此外,滚动指示器仍然存在问题 - 由于某种原因,它会在标题下方传递,就好像标题位于所有内容之上(甚至是指示符)。
有关信息:为方便起见,第一部分没有标题,大小设置为0.另外在第一部分中有3个单元格。
告诉我如何解决这个问题。
不合适。
移动到之后的位置。
滚动指示符在标题下方传递。
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
let cellFirst = Cell()
let randomTexts = ["Aenean dapibus urna a ullamcorper malesuada. Ut tempor. Aenean dapibus urna a ullamcorper malesuada. Ut tempor.",
"Sed venenatis ligula massa, a vulputate ipsum fringilla eget. Ut justo erat, facilisis id rhoncus cursus, fringilla at.",
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum lobortis nibh metus, elementum tempus libero ornare vitae. Etiam sed leo pretium, consectetur turpis non, dapibus purus. Suspendisse potenti. Ut ut eros nunc. Cras nulla justo, porttitor non sapien at, iaculis.",
"Maecenas pellentesque sed magna in congue. Sed non lacus in mi posuere scelerisque. Aenean.",
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras eget ex a velit tincidunt sodales. Donec elementum nisi at enim tempus, et rutrum erat semper. Phasellus ultricies est nec finibus.", "Aenean dapibus urna a ullamcorper malesuada. Ut tempor.",
"Sed venenatis ligula massa, a vulputate ipsum fringilla eget. Ut justo erat, facilisis id rhoncus cursus, fringilla at.",
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum lobortis nibh metus, elementum tempus libero ornare vitae. Etiam sed leo pretium, consectetur turpis non, dapibus purus. Suspendisse potenti. Ut ut eros nunc. Cras nulla justo, porttitor non sapien at, iaculis."]
lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.estimatedItemSize = CGSize(width: view.frame.width, height: 1)
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
collectionView.backgroundColor = .white
collectionView.delegate = self
collectionView.dataSource = self
collectionView.translatesAutoresizingMaskIntoConstraints = false
return collectionView
}()
override func viewDidLoad() {
super.viewDidLoad()
title = "Navigation Bar"
view.addSubview(collectionView)
NSLayoutConstraint.activate([
collectionView.topAnchor.constraint(equalTo: view.topAnchor),
collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
collectionView.leftAnchor.constraint(equalTo: view.leftAnchor),
collectionView.rightAnchor.constraint(equalTo: view.rightAnchor)
])
collectionView.register(Cell.self, forCellWithReuseIdentifier: "cell")
collectionView.register(Header.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "header")
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 5
}
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
switch section {
case 0:
return 3
default:
return randomTexts.count
}
}
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! Cell
cell.descriptionLabel.text = randomTexts[indexPath.row]
return cell
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "header", for: indexPath) as! Header
header.nameTitle = "Header"
return header
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
if section == 0 {
return .zero
}
return CGSize(width: view.frame.width, height: 38)
}
}
类标题:UICollectionReusableView {
var nameTitle: String? {
didSet {
nameLabel.text = nameTitle
}
}
let nameLabel: UILabel = {
let label = UILabel()
label.numberOfLines = 1
label.font = UIFont.systemFont(ofSize: 18)
label.sizeToFit()
label.textAlignment = .left
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
func setupViews() {
backgroundColor = UIColor.blue
addSubview(nameLabel)
NSLayoutConstraint.activate([
nameLabel.topAnchor.constraint(equalTo: topAnchor),
nameLabel.bottomAnchor.constraint(equalTo: bottomAnchor),
nameLabel.leftAnchor.constraint(equalTo: leftAnchor, constant: 16),
nameLabel.rightAnchor.constraint(equalTo: rightAnchor, constant: -16)
])
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class Cell: UICollectionViewCell {
let descriptionLabel: UILabel = {
let label = UILabel()
label.numberOfLines = 0
label.font = UIFont.systemFont(ofSize: 14)
label.sizeToFit()
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
var widthConstraint: NSLayoutConstraint?
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
func setupViews() {
contentView.backgroundColor = #colorLiteral(red: 0.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1)
// Initialization code
self.contentView.translatesAutoresizingMaskIntoConstraints = false
let screenWidth = UIScreen.main.bounds.size.width
widthConstraint = contentView.widthAnchor.constraint(equalToConstant: screenWidth)
widthConstraint?.isActive = true
contentView.autoresizingMask = .flexibleHeight
contentView.addSubview(descriptionLabel)
NSLayoutConstraint.activate([
descriptionLabel.topAnchor.constraint(equalTo: contentView.topAnchor),
descriptionLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
descriptionLabel.leftAnchor.constraint(equalTo: contentView.leftAnchor),
descriptionLabel.rightAnchor.constraint(equalTo: contentView.rightAnchor)
])
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}