帧大小更改时,UICollectionView布局更改

时间:2015-06-11 19:18:52

标签: ios swift uicollectionview uicollectionviewlayout

我正在使用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 个答案:

答案 0 :(得分: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()