下記コードにあるUICollectionViewControllerに、3つのUICollectionViewCellをレジスターしています。
これらをsizeForItemAt indexPath内で異なるCGSizeに設定したいのですが、
上手く設定するための方法が見つかりません。
(width: view.frame.width, height: 400)だったり、
(width: 200, height: 300)だったりと、
異なるCGSizeで設定したいです。
if文やindexPath.itemのcaseでの方法を探してはいるのですが、
なかなか設定できないため質問します。
教えて頂きたいです。
よろしくお願いします。
import UIKit class View1Controller: UICollectionViewController, UICollectionViewDelegateFlowLayout { private let cellId = "cellId" private let cellId2 = "cellId" private let cellId3 = "cellId" override func viewDidLoad() { super.viewDidLoad() 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") collectionView?.register(aCell.self, forCellWithReuseIdentifier: "cellId3") } override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { switch indexPath.item { case 2: return collectionView.dequeueReusableCell(withReuseIdentifier: "cellId3", for: indexPath) as! aCell 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 3 } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: view.frame.width, height: 300) } }
//追記しました。 こちらになります。
//AppDelegateです import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { let layout = UICollectionViewFlowLayout() let view1Controller = View1Controller(collectionViewLayout: layout) window?.rootViewController = UINavigationController(rootViewController: view1Controller) return true } func applicationWillResignActive(_ application: UIApplication) { } func applicationDidEnterBackground(_ application: UIApplication) { } func applicationWillEnterForeground(_ application: UIApplication) { } func applicationDidBecomeActive(_ application: UIApplication) { } func applicationWillTerminate(_ application: UIApplication) { } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/04/14 03:23 編集
2018/04/14 03:37
退会済みユーザー
2018/04/14 04:03 編集
2018/04/14 04:11
退会済みユーザー
2018/04/14 04:28 編集
2018/04/14 05:12 編集
退会済みユーザー
2018/04/14 05:16
2018/04/14 05:23 編集
退会済みユーザー
2018/04/14 05:28 編集
2018/04/14 05:50 編集
退会済みユーザー
2018/04/14 06:27