2016-09-16 25 views
0

各セル内にtableviewセルコレクションビューがあります。コレクションでは画像のみを表示します。 1つ以上存在する可能性があります 5つの画像のレイアウトを表示しても問題ありません。 1つの画像を表示していて、画像の幅と高さに応じてcollectionViewのサイズを変更する必要があるときの問題。だから私はポートレート画像と風景画像を表示することができます。私の現在のレイアウトは、私はあなたがこの問題で私を助けてもらえデフォルトCollectionViewFlowLayout を使用していますUICollectionViewセルを自動サイズ調整する

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 

     switch images.count { 
     case 1: 
      let size = collectionView.frame.size.width 
      return CGSizeMake(size, 250) 
     default: 
      let size = collectionView.frame.size.width/5 
      let offset = CGFloat(5) 
      return CGSizeMake(size - offset, size - offset) 
     } 
    } 

のですか?

+0

上記のコードで直面している問題は何説明できますか? – Natarajan

+0

1枚の写真をコレクションビューに表示すると問題が発生します。写真の内容(縦または横の写真の向きを含む)に応じてサイズ変更のコレクションビューを表示する必要があります – Anton

答えて

0

以下にいくつかの回避策を追加しました。チェックアウトしてください。以下のコードはSwift 3.0にあります。

var collectionView:UICollectionView! 

override func viewDidLoad() { 
    super.viewDidLoad() 
    let layout = UICollectionViewFlowLayout() 
    layout.minimumLineSpacing = 0.0 
    layout.minimumInteritemSpacing = 0.0 

    collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout) 
    collectionView.translatesAutoresizingMaskIntoConstraints = false 
    collectionView.delegate = self 
    collectionView.dataSource = self 
    self.view.addSubview(collectionView) 

    self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[collectionView]|", options: NSLayoutFormatOptions(rawValue:0), metrics: nil, views: ["collectionView":collectionView])) 
    self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[collectionView]|", options: NSLayoutFormatOptions(rawValue:0), metrics: nil, views: ["collectionView":collectionView])) 

    //Register your cell to collectionView over here 

} 

//Add this method to support for orientation changes. 
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { 

    coordinator.animate(alongsideTransition: {(context) -> Void in 

    }) {[weak self] (context) -> Void in 

     self?.collectionView.performBatchUpdates(nil, completion: nil) 
    } 
} 

//Calling collectionView.performBatchUpdates will call the bellow method where you can able to adjust your cell size for orientation change 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 

    switch images.count { 
    case 1: 
     let size = collectionView.frame.size.width 
     return CGSizeMake(size, 250) 
    default: 
     let size = collectionView.frame.size.width/5 
     let offset = CGFloat(5) 
     return CGSizeMake(size - offset, size - offset) 
    } 
} 

また、CollectionViewCellにあるImageViewは、CollectionViewCellのコンテンツビューの左と右の余白に固定する必要があります。また、画像のアスペクト比がcollectionViewの幅の幅と一致することを確認するか、ImageViewのcontentModeをscaleToFillに設定しようとします。

希望すると助かります!

ありがとう!

+0

これはデバイスを回転させるときには問題になりません。ポートレートモードとランドスケープモードの画像がありますか?たとえば、写真の幅が高さよりも小さくなるように、デバイスを縦にして写真を撮影します。私はイメージのexaclyサイズに合うように、そのサイズにセルをサイズ変更する必要があります – Anton

関連する問題