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

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

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

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

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

Q&A

解決済

1回答

1141閲覧

CollectionViewのレイアウトが思い通りにならない。

kazuki_user

総合スコア147

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

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

0グッド

0クリップ

投稿2020/08/28 15:15

編集2020/08/29 09:03

画像とタイトルが、ずれてしまいます。

## わからないこと。

  • imageの制約ができない。
  • タイトル、imageが左寄せにならない。
  • Category Cellの幅 高さを変えても、ビルド時に反映されない
  • CollectionViewのEstimateSizeAutomatic以外にすると、

画像のみ左揃えになり、タイトルは中途半端に左にズレる。

Xibを使用しています。

## レイアウトの警告

'Nature'.width == 100の制約を削除しましたが、引き続きレイアウトは崩れています。

Will attempt to recover by breaking constraint <NSLayoutConstraint:0x60000186f0c0 UILabel:0x7fb077817040'Nature'.width == 100 (active)>

## 参考

イメージ説明

## コード

swift

1 // init 2 let category = Category.init(name: "Nature", id: "abcd", imgUrl: "https://images.unsplash.com/photo-1598556071907-a07e191d0798?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=666&q=80", isActive: true, timeStamp: Timestamp()) 3 categories.append(category) 4 5 // delegate, dataSource 6 collectionView.delegate = self 7 collectionView.dataSource = self 8 collectionView.register(UINib(nibName: Identifiers.CategoryCell, bundle: nil), forCellWithReuseIdentifier: Identifiers.CategoryCell)

swift

1extension HomeVC : UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { 2 3 // numberOfItem (count) 4 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 5 return categories.count 6 } 7 8 // cellForItem (type) 9 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 10 if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Identifiers.CategoryCell, for: indexPath) as? CategoryCell { 11 12 cell.configureCell(category: categories[indexPath.item]) 13 return cell 14 } 15 return UICollectionViewCell() 16 } 17 18 // sizeForItem (width, height) 19 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 20 let width = view.frame.width 21 let cellWidth = (width - 50) / 2 22 let cellHeight = cellWidth * 1.5 23 return CGSize(width: cellWidth, height: cellHeight) 24 } 25}

質問は以上です。
お時間あるときに、ご返信頂けましたら幸いです????

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

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

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

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

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

guest

回答1

0

自己解決

レイアウト作り直したら、いけた????
制約がおかしかった。

投稿2020/08/29 09:03

編集2020/08/29 09:04
kazuki_user

総合スコア147

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問