2017-08-10 8 views
0

UICollectionViewを挟むフッタとヘッダをレンダリングするために2つの補助ビュー(UICollectionReusableView)を使用しています。UICollectionViewController:CollectionViewのサイズに関係なく、スティッキーフッタを作成します。

UICollectionViewが短い(たとえば、単一のセルのみが占有されている)場合、フッタービューがスライドして、ボトムに「スティック」しないという問題があります。

他の記事でもこの問題が解決されました。場合によっては、カスタムレイアウトを使用して固定フッター/ヘッダーを作成することがあります。代わりに、とsectionHeadersPinToVisibleBoundstrueに設定しました。これは一般的にうまくいきます。UICollectionViewが不足するまでです。

これはSwift 3.0(iOS 10.3)、iPhone 6.xシミュレータで発生します。

フッタービューをUICollectionViewControllerの下部に固定するには、どのような方法が推奨されますか?

答えて

0

UICollectionViewControllerの使用に慣れていない場合、私は通常、通常のUIViewControllerを使用し、UICollectionViewDataSourceとUICollectionViewDelegateプロトコルを拡張してUICollectionViewをスティックします。

UICollectionViewControllerが必要な場合は、同じことができますが、ビューコントローラ自体を埋め込むことができます。

UIViewControllerを使用すると、制約付きのビューを簡単に配置できます。

+0

。私がUICollectionViewControllerを使って "組み込みの"フッター/ヘッダーを選択したのは、そのコントローラがすぐに使えるということです。そのコントローラーは私たちの問題のためにうまく動作します。私はジェネリックUIViewControllerはおそらく正常に動作すると思うが、少し足の仕事が必要な場合があります... –

0

ok ..下のリンクからコードを更新しようとしました。それは動作します。私たちは、おそらくあなたの提案、次のことになります

https://teamtreehouse.com/community/add-sticky-footer-to-uicollectionview-in-swift

class StickyFooter : UICollectionViewFlowLayout { 


var footerIsFound    : Bool = false 
var UICollectionAttributes : [UICollectionViewLayoutAttributes]? 


override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool { 
    return true 
} 


override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? 
{ 
    UICollectionAttributes = super.layoutAttributesForElements(in: rect) 

    for attributes in UICollectionAttributes! { 

     if let type = attributes.representedElementKind { 

      if type == UICollectionElementKindSectionFooter 
      { 
       footerIsFound = true 
       updateFooter(attributes: attributes) 
      } 
     } 
    } 

    if (!self.footerIsFound) { 

     let newItem = self.layoutAttributesForSupplementaryView(ofKind: UICollectionElementKindSectionFooter, at : NSIndexPath(row: self.UICollectionAttributes!.count, section: 0) as IndexPath) 


     UICollectionAttributes?.append(newItem!) 

    } 

    return UICollectionAttributes 
} 

override func layoutAttributesForSupplementaryView(ofKind elementKind: String, at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? 
{ 
    let attributes = UICollectionViewLayoutAttributes(forSupplementaryViewOfKind: elementKind, with: indexPath) 

    attributes.size = CGSize(width: self.collectionView!.bounds.size.width, height: 75) //your footer size 

    if elementKind.isEqualToString(find: UICollectionElementKindSectionFooter) 
    { 
     updateFooter(attributes: attributes) 
    } 
    return attributes 
} 

func updateFooter(attributes : UICollectionViewLayoutAttributes){ 
    let currentBounds = self.collectionView?.bounds 
    attributes.zIndex = 1024 
    attributes.isHidden = false 
    let yOffset = currentBounds!.origin.y + currentBounds!.size.height - attributes.size.height/2.0 
    attributes.center = CGPoint(x: currentBounds!.midX, y: yOffset) 

}} 
+0

あなたのcollectionviewflowレイアウトにクラスStickyFooterを割り当てる –

関連する問題