2016-12-28 9 views
1

私はViewControllerを持っていますが、2つありますが、1つはセルのほうがisPagingEnabled、もう1つはフルフレーム幅のためです。collectionView 3アイテムです。どうやってやるの ?ページングのためのsizeForItemAtのコレクションビューを1つだけ設定するにはどうすればよいですか?

MenuCollectionView:それは完全に

func setupMenuCollection(){   
      if let flowLayout = menuCollectionView?.collectionViewLayout as? UICollectionViewFlowLayout { 
       flowLayout.scrollDirection = .horizontal 
       flowLayout.minimumLineSpacing = 0 
      } 

      menuCollectionView?.backgroundColor = UIColor.white 
      menuCollectionView?.contentInset = UIEdgeInsetsMake(50, 0, 0, 0) 
      menuCollectionView?.scrollIndicatorInsets = UIEdgeInsetsMake(50, 0, 0, 0) 

      menuCollectionView?.isPagingEnabled = true 

     } 

作業はmanubarCollectionViewためである:それはここにいないelseステートメントので、機能していません。

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

      return CGSize(width: view.frame.width/3, height: manubarCollectionView.frame.height) 
     } 

    } 
+0

をチェックし、セルの幅と高さを設定するために使用されます? –

+0

menuCollectionViewおよびmanubarCollectionView –

答えて

3

collectionViewセルサイズがelse blockを追加し、CGSizeを返すか、直接if blockCGSizeを返すのいずれかのためにそれは、簡単です。

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

     return CGSize(width: view.frame.width/3, height: manubarCollectionView.frame.height) 
    } 
    else { 
     //return cell size for menuCollectionView 
     return collectionView.frame.size 
    } 
} 

OR

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

     return CGSize(width: view.frame.width/3, height: manubarCollectionView.frame.height) 
    } 

    //return cell size for menuCollectionView 
    return collectionView.frame.size 
} 

注:あなたは2 collectionViewを区別するためにcollectionViewのすべてdataSourcedelegate方法でこのタイプの条件を配置する必要があります。方法上記

+0

@cristanlika編集済みの回答を確認して、明確に理解し、上記のコメントを詳細に指定します。 –

+0

@cristanlika書かれているのはあなたのmenuCollectionViewのセルですか? –

1

参照Cristan、

menuCollectionView?.contentInset = UIEdgeInsetsMake(50, 0, 0, 0) 

は、二つのセル間のスペースを設定するために使用されます。

そして方法collectionViewLayout以下 を使用すると、2つのコレクションビューのために、異なる細胞を処理したい場合は、2番目のcollectionViewある場合は

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
    if manubarCollectionView == collectionView { 
     return CGSize(width: view.frame.width/3, height: manubarCollectionView.frame.height) 
    } 
    else 
    { 
     return CGSize(width: 100, height: 100) 
    } 
} 

その後、

how to use two CollectionView on same view controller

関連する問題