2015-06-11 51 views
0

我正在使用UICollectionView,但我有一個奇怪的行爲。當幀大小發生變化時,UICollectionView佈局發生變化

我想要一個水平滾動,小單元格高度(小於UICollectionView高度)。 點擊一個按鈕將增加集合的高度。

問題是我的細胞佈局也在變化。 我希望集合視圖高度隨着單元停留在相同位置而增加。 這裏附加了2個屏幕截圖(觸摸按鈕之前/之後)。 這裏是我的代碼:

var smallLayout:UICollectionViewFlowLayout = UICollectionViewFlowLayout(); 
smallLayout.scrollDirection = .Horizontal 
var itemHeight = 100 
smallLayout.itemSize = CGSizeMake(150, itemHeight); 
var bottomDist:CGFloat = 1 
var topDist = collHeight - itemHeight - bottomDist 
smallLayout.sectionInset = UIEdgeInsets(top: topDist, left: 2, bottom: bottomDist, right: 2) 
smallLayout.minimumInteritemSpacing = 15 
smallLayout.minimumLineSpacing = 10 

因此,觸摸按鈕,我有以下代碼時:

let viewHeight = CGRectGetHeight(self.view.frame) 
let viewWidth = CGRectGetWidth(self.view.frame) 
var collHeight:CGFloat = 600 
var collYPosition = viewHeight-CGFloat(collHeight)-20 
var rect = CGRectMake(0, CGFloat(collYPosition), CGFloat(viewWidth), CGFloat(collHeight)) 
dishCollectionView.frame = rect 

所以,我不明白爲什麼我的佈局正在發生變化。

我想讓我的細胞在底部。

enter image description here enter image description here

回答

1

或許,如果您重置sectionInset的頂部,以適應更多的高度,即更新topDist計算。嘗試在觸摸代碼上添加如下內容:

var bottomDist:CGFloat = 1 
var topDist = collHeight - itemHeight - bottomDist 
dishCollectionView.collectionViewLayout.sectionInset = UIEdgeInsets(top: topDist, left: 2, bottom: bottomDist, right: 2) 
dishCollectionView.collectionViewLayout.invalidateLayout() 
+0

就是這樣。有用。謝謝。 –

相關問題