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

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

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

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

Q&A

解決済

1回答

1119閲覧

collectionViewのLayoutが崩れる。

atk_721

総合スコア62

Swift

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

0グッド

0クリップ

投稿2020/05/14 07:25

前提・実現したいこと

iphone8 でのcollectionViewの表示。
イメージ説明

iphone11 pro でのcollectionViewの表示。
イメージ説明

iphone8 plus でcollectionViewCellを3つ並べたい。

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

iphone11 proではcollectionViewCellが3列並ぶのですが、
iphne8 plusになるとなぜか、layoutが崩れて2列しか並ばない。

デバイスの違いで、描画が変わってしまいます。

該当のソースコード

import UIKit class CategoryCollectionViewController: UIViewController { //MARK: IBoutlet Vars @IBOutlet weak var categoryCollectionView: UICollectionView! //MARK: Vars var categoryArray: [Category] = [] //MARK: Let private let sectionInsets = UIEdgeInsets(top: 20.0, left: 10.0, bottom: 20.0, right: 10.0) private let itemsPerRow: CGFloat = 3 //MARK: View LifeCycle override func viewDidLoad() { super.viewDidLoad() categoryCollectionView.delegate = self categoryCollectionView.dataSource = self } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) //TransParency NavigationBar self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default) self.navigationController?.navigationBar.shadowImage = UIImage() } override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) loadCategories() } //MARK: Download Categories private func loadCategories(){ downloadCategoriesFromFireStore { (allCategories) in self.categoryArray = allCategories self.categoryCollectionView.reloadData() print("all Category Count : (self.categoryArray.count)") } } } //MARK: UICollectionView Delegate DataSource extension CategoryCollectionViewController: UICollectionViewDelegate, UICollectionViewDataSource{ func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return categoryArray.count } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CategoryCollectionViewCell cell.toFields(categoryArray[indexPath.row]) return cell } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { performSegue(withIdentifier: "CategoryItemSegue", sender: categoryArray[indexPath.row]) } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "CategoryItemSegue"{ let vc = segue.destination as! ItemsTableViewController vc.category = sender as? Category return } print("categoryItemSegue can not find") } } //MARK: CollectionView FlowLayout extension CategoryCollectionViewController: UICollectionViewDelegateFlowLayout { func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { let paddingSpace = sectionInsets.left * (itemsPerRow + 1) let availableWidth = UIScreen.main.bounds.width - paddingSpace let withPerItem = availableWidth / itemsPerRow //cellの大きさを返す return CGSize(width: withPerItem, height: withPerItem) } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { return sectionInsets } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { return sectionInsets.left } }

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

ここにより詳細な情報を記載してください。

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

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

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

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

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

guest

回答1

0

ベストアンサー

storyboard上のspacingの設定が残ってるかな?

イメージ説明

余白に対する設定が脆弱かと思います、
自分はちょっとforCellとかforLinesとか失念してしまってるので、調べてみていだだけるでしょうか?

投稿2020/05/14 09:44

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

atk_721

2020/05/14 10:38

StoryBoard上でMinSpasingのForCellとForLinesを10としていましたが、0にすると期待どうりの動きに変わりました!CollectionViewについてよく理解できていないので調べてみます!ありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問