質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

780閲覧

UICollectionViewControllerの上にcollectionViewCellを2つ乗せたいです。

退会済みユーザー

退会済みユーザー

総合スコア0

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2018/04/11 16:15

編集2018/04/11 16:23

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) } }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

下記でいかがでしょうか?ご質問の内容ですとコンパイルエラーになるので一部変更しています。

import UIKit class ViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout { private let cellId = "cellId" private let cellId2 = "cellId" override func viewDidLoad() { super.viewDidLoad() //collectionView?.backgroundColor = .white let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() layout.minimumInteritemSpacing = 0 layout.minimumLineSpacing = 0 collectionView?.collectionViewLayout = layout collectionView?.register(HogeCell.self, forCellWithReuseIdentifier: "cellId") collectionView?.register(HugaCell.self, forCellWithReuseIdentifier: "cellId2") } 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 2 } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: view.frame.width, height: 400) } }
import UIKit class HogeCell: UICollectionViewCell { required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder)! } override init(frame: CGRect) { super.init(frame: frame) self.contentView.backgroundColor = .orange } }
import UIKit class HugaCell: UICollectionViewCell { required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder)! } override init(frame: CGRect) { super.init(frame: frame) self.contentView.backgroundColor = .blue } }

投稿2018/04/11 22:17

編集2018/04/11 22:24
newmt

総合スコア1277

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2018/04/12 10:32

無事実現できました!anchorやcellのインスタンスは必要なかったんですね。 これからはもっとシンプルに実装できる様に進めて行きたいです。 いつも勉強になります ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問