複数のセクションでコレクションビューを作成しました。ヘッダービューとコレクションビューのセルを同じサイズにします。コレクションビューのヘッダーとセルを同じサイズにする方法
私はいくつかのことを試してみた:
- は、細胞またはヘッダに影響を及ぼさないように思われるコレクションビュー、上のインセットを設定します。
- 変更されたreferenceSizeForHeaderInSectionも効果がないようです。
- Xibのビューのサイズを再利用可能なビューに変更しました。
- Xib内のUILabelを上、左、右、下から0に制限し、幅を300に設定しましたが、これはオーバーライドされているようです(下の図の黒い部分はアプリ名です) 。以下は
Screenshot of constraints being broke
私の方法のいくつかである:
のviewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
collectionView.contentInset = UIEdgeInsets(top: 30, left: 30, bottom: 0, right: 30)
collectionViewHeightAnchor.constant = view.frame.height/2
let nib = UINib(nibName: "CollectionReusableView", bundle: nil)
collectionView.register(nib, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "groceryHeader")
}
コレクションビューのデリゲート/データソースとヘッダの設定
// MARK: - Header
func numberOfSections(in collectionView: UICollectionView) -> Int {
return foodItems.count
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: 120, height: 45)
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let (key, _) = foodItems[indexPath.section]
let cell = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "groceryHeader", for: indexPath) as! CollectionReusableView
cell.lbl.text = key
return cell
}
// MARK: - Cell
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return foodItems[section].1.count
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSize(width: 120, height: 45)
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "foodCell", for: indexPath)
if let cell = cell as? FoodItemCell {
let (_, val) = foodItems[indexPath.section]
cell.titleLbl.text = val[indexPath.row].text
cell.quantityLbl.text = val[indexPath.row].quantity
cell.postId = val[indexPath.row].id
if let arr = UserDefaults.standard.getCheckedIds() {
cell.checkBoxImg.setImage(arr.contains(val[indexPath.row].id) ? UIImage(named: "checked") : UIImage(named: "unchecked"), for: .normal)
}
}
return cell
}
は、事前にありがとうございます。
ありがとう、それはトリックを行った。 –