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

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

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

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

Swift

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

Q&A

解決済

1回答

848閲覧

UICollectionViewCellの最小サイズを設定したい

daiishi

総合スコア6

iOS

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

Swift

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

0グッド

0クリップ

投稿2021/08/06 07:19

編集2021/08/07 02:14

UICollectionViewを使用して下記画像のような左寄せのタグ一覧を実装しています。
Cellサイズを指定するのに、UICollectionViewFlowLayoutで
estimatedItemSize = UICollectionViewFlowLayout.automaticSize
を使用しています。
その際、概ね期待通りのCellサイズになるのですが、一文字LabelのCellサイズだけもう少し大きくしたいと思っています。
下記画像で言う「豆」「栗」「鯛」などの一文字ラベルのCellサイズを「焼肉」「そば」などの二文字ラベルのCellサイズを同じにしたいです。

一文字のデータの量端に半角スペースを入れてみたりしたのですが、期待通りの大きさとは違うのと
、できれば最小のCellサイズを指定できないかと考えております。
いろいろと検索したのですが、解決策がみつけることができませんでした。

お知恵をお貸しいただけないでしょうか。
よろしくお願いいたします。
イメージ説明

Swift

1//TagCollectionViewController.swift 2 3import UIKit 4 5class TagCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout { 6 7 let cellId = "cellId" 8 let headerId = "headerId" 9 10 override func viewDidLoad() { 11 super.viewDidLoad() 12 collectionView.backgroundColor = #colorLiteral(red: 0.9410743117, green: 0.9412353635, blue: 0.9410640597, alpha: 1) 13 14 collectionView.register(TagHeader.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: headerId) 15 collectionView.register(TagCollectionViewCell.self, forCellWithReuseIdentifier: cellId) 16 collectionView.contentInset = .init(top: 0, left: 10, bottom: 10, right: 10) 17 } 18 19 var tagCategory: [String] = [ 20 "レストラン", 21 "レストラン", 22 "レストラン", 23 "レストラン", 24 "レストラン", 25 ] 26 27 var tagItems0: [String] = [ 28 "ビアガーデン", "豆", "栗", "焼肉", "鯛", "ステーキ", "そば", "バイキング・ビュッフェ" 29 ] 30 31 var tagItems1: [String] = [ 32 "ビアガーデン", "うなぎ", "韓国料理", "焼肉", "焼き鳥", "ステーキ", "そば", "バイキング・ビュッフェ" 33 ] 34 35 var tagItems2: [String] = [ 36 "ビアガーデン", "うなぎ", "韓国料理", "焼肉", "焼き鳥", "ステーキ", "そば", "バイキング・ビュッフェ" 37 ] 38 39 var tagItems3: [String] = [ 40 "ビアガーデン", "うなぎ", "韓国料理", "焼肉", "焼き鳥", "ステーキ", "そば", "バイキング・ビュッフェ" 41 ] 42 43 var tagItems4: [String] = [ 44 "ビアガーデン", "うなぎ", "韓国料理", "焼肉", "焼き鳥", "ステーキ", "そば", "バイキング・ビュッフェ" 45 ] 46 47 class TagCollectionViewCell: UICollectionViewCell { 48 49 let tagLabel: UILabel = { 50 let label = UILabel() 51 label.text = "sample" 52 label.font = .systemFont(ofSize: 12) 53 return label 54 }() 55 56 override init(frame: CGRect) { 57 super.init(frame: frame) 58 addSubview(tagLabel) 59 tagLabel.fillSuperview(padding: .init(top: 8, left: 12, bottom: 8, right: 12)) 60 } 61 62 required init?(coder: NSCoder) { 63 fatalError("init(coder:) has not been implemented") 64 } 65 } 66 67 68 class TagHeader: UICollectionReusableView { 69 70 let headerLabel: UILabel = { 71 let label = UILabel() 72 label.text = "headerSample" 73 label.font = .boldSystemFont(ofSize: 12) 74 return label 75 }() 76 77 override init(frame: CGRect) { 78 super.init(frame: frame) 79 80 addSubview(headerLabel) 81 headerLabel.fillSuperview() 82 } 83 84 required init?(coder: NSCoder) { 85 fatalError("init(coder:) has not been implemented") 86 } 87 88 } 89 90 91 override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { 92 let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerId, for: indexPath) as! TagHeader 93// header.backgroundColor = .gray 94 header.headerLabel.text = tagCategory[indexPath.section] 95 return header 96 } 97 98 99 override func numberOfSections(in collectionView: UICollectionView) -> Int { 100 return tagCategory.count 101 } 102 103 104 override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 105 switch section { 106 case 0: return tagItems0.count 107 case 1: return tagItems1.count 108 case 2: return tagItems2.count 109 case 3: return tagItems3.count 110 default: return tagItems4.count 111 } 112 113 } 114 115 override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 116 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! TagCollectionViewCell 117 switch indexPath.section { 118 case 0: cell.tagLabel.text = tagItems0[indexPath.item] 119 case 1: cell.tagLabel.text = tagItems1[indexPath.item] 120 case 2: cell.tagLabel.text = tagItems2[indexPath.item] 121 case 3: cell.tagLabel.text = tagItems3[indexPath.item] 122 default: cell.tagLabel.text = tagItems4[indexPath.item] 123 } 124 cell.layer.cornerRadius = 12 125 cell.backgroundColor = .white 126 return cell 127 } 128 129extension UIView { 130 func fillSuperview(padding: UIEdgeInsets = .zero) { 131 translatesAutoresizingMaskIntoConstraints = false 132 if let superviewTopAnchor = superview?.topAnchor { 133 topAnchor.constraint(equalTo: superviewTopAnchor, constant: padding.top).isActive = true 134 } 135 136 if let superviewBottomAnchor = superview?.bottomAnchor { 137 bottomAnchor.constraint(equalTo: superviewBottomAnchor, constant: -padding.bottom).isActive = true 138 } 139 140 if let superviewLeadingAnchor = superview?.leadingAnchor { 141 leadingAnchor.constraint(equalTo: superviewLeadingAnchor, constant: padding.left).isActive = true 142 } 143 144 if let superviewTrailingAnchor = superview?.trailingAnchor { 145 trailingAnchor.constraint(equalTo: superviewTrailingAnchor, constant: -padding.right).isActive = true 146 } 147 } 148} 149 150

Swift

1//LeftAlignedCollectionViewFlowLayout.swift 2 3import UIKit 4 5class LeftAlignedCollectionViewFlowLayout: UICollectionViewFlowLayout { 6 7 override init() { 8 super.init() 9 estimatedItemSize = UICollectionViewFlowLayout.automaticSize 10 11 } 12 13 required init?(coder: NSCoder) { 14 fatalError("init(coder:) has not been implemented") 15 } 16 17 override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? { 18 let attributes = super.layoutAttributesForElements(in: rect) 19 20 var leftMargin = sectionInset.left 21 var maxY: CGFloat = -1.0 22 attributes?.forEach { layoutAttribute in 23 24 guard layoutAttribute.representedElementCategory == .cell else { return } 25 26 if layoutAttribute.frame.origin.y >= maxY { 27 leftMargin = sectionInset.left 28 } 29 30 layoutAttribute.frame.origin.x = leftMargin 31 32 leftMargin += layoutAttribute.frame.width + minimumInteritemSpacing 33 maxY = max(layoutAttribute.frame.maxY , maxY) 34 } 35 36 return attributes 37 } 38} 39

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

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

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

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

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

hoshi-takanori

2021/08/06 20:00

fillSuperview が何をしてるか分かりませんが、そのあたりで最小の Cell サイズの制約を付ければ良いのでは。
hoshi-takanori

2021/08/07 07:06

たぶんこんな感じ? tagLabel.widthAnchor.constraint(greaterThanOrEqualToConstant: 20).isActive = true
daiishi

2021/08/07 08:26

ご指摘通り tagLabel.widthAnchor.constraint(greaterThanOrEqualToConstant: 20).isActive = true で解決いたしました! UICollectionViewFlowLayoutでcellの大きさを指定しようとしていたことと .constraint(greaterThanOrEqualToConstant: 20)を知らなかったため ハマっていました。 助かりました。ありがとうございます!
guest

回答1

0

自己解決

hoshi-takanoriさんにコメント頂いた通り
tagLabel.widthAnchor.constraint(greaterThanOrEqualToConstant: 20).isActive = true
で解決しました。

投稿2021/08/09 10:49

daiishi

総合スコア6

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.45%

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

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

質問する

関連した質問