2017-05-08 8 views
0

UICollectionViewFlowLayoutのカスタムサブクラスがあり、セルのサイズを常に画面の幅に更新しようとしています。私は風景に回転するとき、セルが正しく更新されていません。ここに私のサブクラスです:UICollectionViewFlowLayoutサブクラスのセル幅の更新に関する問題

// MARK: - Constants 

private let EdgeInset: CGFloat = 10 
private let CellSpacing: CGFloat = 10 
private let LineSpacing: CGFloat = 10 
private let CellHeight: CGFloat = 50 

// MARK: - FullWidthLayout 

class FullWidthLayout: UICollectionViewFlowLayout { 

    override init() { 
     super.init() 
     commonInit() 
    } 

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
     commonInit() 
    } 

    func commonInit() { 
     minimumLineSpacing = LineSpacing 
     minimumInteritemSpacing = CellSpacing 
     sectionInset = UIEdgeInsets(top: EdgeInset, left: EdgeInset, bottom: EdgeInset, right: EdgeInset) 
    } 
} 

// MARK: Layout Methods 

extension FullWidthLayout { 

    override func prepare() { 
     super.prepare() 

     if let view = collectionView { 
      let availableWidth = view.bounds.width - EdgeInset * 2 
      itemSize = CGSize(width: availableWidth, height: CellHeight) 

      print("Item size: \(itemSize)") 
     } 
    } 
} 

は、ここでコンソールの出力です:

Item size: (300.0, 50.0) 
Item size: (548.0, 50.0) 

そして、ここでは、シミュレータで何が起こっているのかです:

portrait version of my app

landscape version of my app

アイテムのサイズ正しく設定されているようですprepare()メソッドですが、セルはコレクションビューで更新されません。あなたが風景で始まり、肖像画に移動すると、例外がスローされます。

紛失しているものがありますか?場合によっては、ベースUICollectionViewFlowLayoutitemSizeを無視していますか?ここで

はGitHubの上のプロジェクトへのリンクです: CollectionViewRotate

答えて

0

があなたのViewControllerでこれを追加

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
     return CGSize(width: collectionView.frame.size.width/ 2 - 10, height: 100) // chnage as per your need ...10 is the space between cell 
    } 

override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) { 
    myCollectionView!.reloadData() 
} 
レイアウト上のアイテムの大きさを把握することはできません場合にのみ、そのデリゲートメソッドを実装する必要が
+0

その自分の。レイアウトで独自のアイテムサイズを設定することは可能です。 – Mark

関連する問題