UICollectionViewControllerの上にcollectionViewCellを2つ乗せたいです。
collectionViewCellは
・HogeCell(backgroundColorはオレンジ)
・HugaCell(backgroundColorはブルー)
です。
heightをそれぞれ400にして、丁度上半分オレンジ 下半分ブルーのビューを作りたいのですが、うまく行きません。
エラー 'negative sizes are not supported in the flow layout'が発生します。
原因が分からない為質問します。
どうぞよろしくお願いします。
import UIKit class UICollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout { private let cellId = "cellId" private let cellId2 = "cellId" override func viewDidLoad() { super.viewDidLoad() //collectionView?.backgroundColor = .white collectionView?.register(HogeCell.self, forCellWithReuseIdentifier: "cellId") collectionView?.register(HugaCell.self, forCellWithReuseIdentifier: "cellId2") setupViews() } let hogeCell: HogeCell = { let hoge = HogeCell() hoge.translatesAutoresizingMaskIntoConstraints = false return hoge }() let hugaCell: HugaCell = { let huga = HugaCell() huga.translatesAutoresizingMaskIntoConstraints = false return huga }() func setupViews() { [hogeCell, hugaCell].forEach { view.addSubview($0) } hogeCell.anchor (top: view.topAnchor, leading: view.leadingAnchor, bottom: cateCell.topAnchor, trailing: view.trailingAnchor, padding: .init(top: 0, left: 0, bottom: 0, right: 0), size: .init(width: view.frame.width, height: 400)) hugaCell.anchor (top: hogeCell.bottomAnchor, leading: view.leadingAnchor, bottom: view.bottomAnchor, trailing: view.trailingAnchor, padding: .init(top: 0, left: 0, bottom: 0, right: 0), size: .init(width: view.frame.width, height: 400)) } override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { switch indexPath.item { case 1: return collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! hogeCell default: return collectionView.dequeueReusableCell(withReuseIdentifier: "cellId2", for: indexPath) as! hugaCell } } override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 1 } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: view.frame.width, height: view.frame.height) } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/04/12 10:32