Vì vậy, đây là một vấn đề thú vị, chúng tôi tìm thấy với UICollectionViewFlowLayout
trên iOS 10 (vẫn còn là một vấn đề ngày 11) và sử dụng UICollectionViewFlowLayoutAutomaticSize
cho estimatedItemSize.iOS 10/11 UICollectionViewFlowLayout sử dụng kết quả UICollectionViewFlowLayoutAutomaticSize theo quan điểm bổ sung chân lệch
Chúng tôi thấy rằng việc sử dụng UICollectionViewFlowLayoutAutomaticSize
về kết quả estimatedItemSize trong giao diện bổ sung chân lơ lửng phía trên vài tế bào đáy. (Red/Pink là tiêu đề, màu xanh lá cây là chân.)
đây là mã VC của một ứng dụng mẫu:
import UIKit
class ViewController: UIViewController {
// MARK: Properties
let texts: [String] = [
"This is some text",
"This is some more text",
"This is even more text"
]
// MARK: Outlets
@IBOutlet var collectionView: UICollectionView! {
didSet {
self.collectionView.backgroundColor = .orange
}
}
// MARK: Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
// Layout
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
if #available(iOS 10.0, *) {
layout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize
} else {
layout.estimatedItemSize = CGSize(width: self.collectionView.bounds.width, height: 50)
}
self.collectionView.collectionViewLayout = layout
// Register Cells
self.collectionView.register(UINib(nibName: "TextCell", bundle: nil), forCellWithReuseIdentifier: String(describing: TextCell.self))
self.collectionView.register(UINib(nibName: "SectionHeader", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: String(describing: SectionHeader.self))
self.collectionView.register(UINib(nibName: "SectionFooter", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: String(describing: SectionFooter.self))
self.collectionView.reloadData()
}
}
// MARK: - UICollectionViewDelegateFlowLayout Methods
extension ViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: self.collectionView.bounds.width, height: 90)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
return CGSize(width: self.collectionView.bounds.width, height: 90)
}
}
// MARK: - UICollectionViewDataSource Methods
extension ViewController: UICollectionViewDataSource {
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.texts.count
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: String(describing: TextCell.self), for: indexPath)
if let textCell = cell as? TextCell {
let text = self.texts[indexPath.row]
textCell.configure(text: text)
}
return cell
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionElementKindSectionHeader:
return collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: String(describing: SectionHeader.self), for: indexPath)
case UICollectionElementKindSectionFooter:
return collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: String(describing: SectionFooter.self), for: indexPath)
default:
return UICollectionReusableView()
}
}
}
// MARK: - UICollectionViewDelegate Methods
extension ViewController: UICollectionViewDelegate {
}
có ai quản lý để có được UIColle ctionViewFlowLayoutAutomaticSize hoạt động trên iOS 10 với các chế độ xem đầu trang và chân trang bổ sung? Nếu tôi thêm kích thước vào tệp ước tínhItemSize thì nó có vẻ hoạt động, nhưng tôi muốn biết nếu có lỗi trong việc sử dụng tính năng iOS 10 mới hoặc nếu tôi đang sử dụng nó không chính xác.
Lỗi nộp cho Apple có ID:
CẬP NHẬT: Đây vẫn dường như là một vấn đề trên 10,3 Beta 1
UPDATE 2: Đây vẫn dường như là một vấn đề trong iOS 11 Beta 1
Bạn cần cung cấp ước tínhItemSize afaik. Nó không phải là tùy chọn – Lefteris
Nó đang được thiết lập, nó chỉ khác nhau giữa iOS 10 và trước đó. – dlbuckley
Bạn có tìm thấy giải pháp cho việc này không? –