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

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

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

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

Swift

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

Q&A

0回答

430閲覧

collectionViewCellの高さを可変にするとレイアウトがくずれる

nishimu

総合スコア26

Xcode

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

Swift

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

0グッド

0クリップ

投稿2020/08/06 12:16

編集2020/08/14 09:25

前提・実現したいこと

collectionViewのcellの高さをレイアウトを崩さず可変にしたいです。
(このときのレイアウトとはセルに影をつけることができ、セルの高さが可変で、cellの幅を画面の両端から8ずつ空けたものです)

発生している問題・エラーメッセージ

cellにつけている影の挙動がおかしくなります。
cellの幅を画面の両端から8ずつ開けたいのですが、こちらで指定することができず困っています。
また、質問に答えられる方がおらず、困っております
イメージ説明

該当のソースコード

swift

1import UIKit 2 3class ViewController: UIViewController, UICollectionViewDelegateFlowLayout,UICollectionViewDataSource { 4 5 @IBOutlet weak var collectionView: UICollectionView! 6 let array = ["labellabellabel","labellabellabellabellabellabellabellabellabellabellabellabellabellabellabellabellabellabel","labellabellabellabellabellabellabellabellabellabellabellabellabellabellabellabellabellabellabellabellabellabellabellabellabellabellabellabellabellabel", 7 "labellabellabellabellabellabellabellabellabellabellabellabellabellabellabellabellabellabellabellabellabellabellabellabellabellabellabellabellabellabel"] 8 9 override func viewDidLoad() { 10 super.viewDidLoad() 11 12 collectionView.delegate = self 13 collectionView.dataSource = self 14 15 if let flowlayout = self.collectionView.collectionViewLayout as? UICollectionViewFlowLayout{ 16 flowlayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize 17 } 18 19 } 20 21 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 22 return array.count 23 } 24 25 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 26 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell 27 cell.label_1.text = array[indexPath.row] 28 cell.label_2.text = array[indexPath.row] 29 30 cell.layer.cornerRadius = 4.0 31 cell.layer.borderWidth = 1.0 32 cell.layer.borderColor = UIColor.clear.cgColor 33 cell.layer.masksToBounds = false 34 35 cell.layer.shadowColor = UIColor.gray.cgColor 36 cell.layer.shadowOffset = CGSize(width: 0 , height: 1.0) 37 cell.layer.shadowRadius = 4.0 38 cell.layer.shadowOpacity = 1.0 39 cell.layer.masksToBounds = false 40 cell.layer.shadowPath = UIBezierPath(roundedRect: cell.bounds, 41 cornerRadius: cell.layer.cornerRadius).cgPath 42 43 return cell 44 } 45 46 47}

swift

1import UIKit 2 3class CollectionViewCell: UICollectionViewCell { 4 @IBOutlet weak var label_1: UILabel! 5 @IBOutlet weak var label_2: UILabel! 6 @IBOutlet weak var view_1: UIView! 7 @IBOutlet weak var widhtConstraint: NSLayoutConstraint! 8 9 override func awakeFromNib() { 10 super.awakeFromNib() 11 12 // 幅が自動調整されないようにする 13 self.contentView.translatesAutoresizingMaskIntoConstraints = false 14 let screenWidth = UIScreen.main.bounds.size.width 15 widhtConstraint.constant = screenWidth 16 17 } 18}

イメージ説明
widthConstraintはlabel_1についての幅の制約です

試したこと

以下のサイトなどを参考にしました
https://qiita.com/usagimaru/items/e0a4c449d4cdf341e152
https://stackoverflow.com/questions/28161839/uicollectionview-dynamic-cell-height

補足情報(FW/ツールのバージョンなど)

Xcode11.6

ご教授願います

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問