2017-02-12 95 views
0

我正在iOS應用程序,我想顯示UIScrollView與兩個UILabelsUIImageView和底部UICollectionView。現在我不知道如何在自動佈局中做到這一點,誰能幫助我?滾動查看CollectionView

我試過只是增加一切,並設置約束彼此,但我記得UIScrollView需要計算內在的內容高度。我怎樣才能創建這個滾動視圖?

+0

請確保您有從頂視圖一個連接鏈到你的滾動視圖的底部。如果您將間距設置爲最頂層項目的scrollView頂部,爲最低項目設置底部間距,在介入項目之間設置空間,並且所有項目都有指定高度,iOS將能夠計算您的scrollView的大小。 – vacawama

+0

我也建議你添加一個容器視圖來保存你的scrollView的全部內容。這樣可以很容易地將滾動限制在所有設備上的垂直位置。請參閱:http://stackoverflow.com/a/28048228/1630618 – vacawama

+0

好吧,但如果我設置我的collectionView.height = 200,這意味着我看不到200點以下的項目,對不對? – jbehrens94

回答

2

在這種情況下,我會避免將UICollectionView嵌入到UIScrollView中。使用UICollectionReusableView子類將UILabelUIImageView添加到節標題中會更容易。

步驟如下:

  1. 節頭添加到UICollectionView

enter image description here

  • 建立您的節頭的視圖類子類UICollectionReusableView。設置自定義類和重用頭可重複使用的視圖的標識符:
  • enter image description hereenter image description here

  • 鋪陳您的標題視圖。連接你的網點。
  • 執行viewForSupplementaryElementOfKind方法。

    override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { 
    
        // If you also use footers: 
        // use a switch statement on the 'kind' argument to 
        // decide which view to dequeue. 
    
        let view = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "Header", for: indexPath) 
    
        // set up your header view 
    
        return view 
    }