在我的CollectionView的高度在运行时更改时更新UICollectionViewFlowLayout

时间:2016-04-28 23:45:51

标签: ios uicollectionview

我的MyCollectionView有一个约束条件:

BottomLayoutGuide.Top = MyCollectionView.Bottom + 100

在我的IB中,但这个约束的常数会根据键盘的高度而变化

func keyboardWillShow(notification: NSNotification) {
        let keyboardHeight = getKeyboardHeight(notification)
        keyboardHeightConstraint.constant = keyboardHeight + 20

        self.cardCollectionView.configureCollectionView()
        self.cardCollectionView.reloadInputViews()
    }

configureCollectionView的位置:

func configureCollectionView() {

    self.backgroundColor = UIColor.clearColor()

    // Create the layout
    let space = 10.0 as CGFloat
    let flowLayout = UICollectionViewFlowLayout()

    let width = (self.frame.size.width) - 4 * space
    let height = (self.frame.size.height)

    let edgeInsets = UIEdgeInsetsMake(0, 2 * space, 0, 2 * space)

    // Set top and bottom margins if scrolling horizontally, left and right margins if scrolling vertically
    flowLayout.minimumLineSpacing = space
    flowLayout.minimumInteritemSpacing = 0

    // Set horizontal scrolling
    flowLayout.scrollDirection = .Horizontal

    // Set edge insets
    flowLayout.sectionInset = edgeInsets
    flowLayout.itemSize = CGSizeMake(width, height)
    print(self.frame)
    print(flowLayout.itemSize)
    self.setCollectionViewLayout(flowLayout, animated: true)

    // Always allow it to scroll
    self.alwaysBounceHorizontal = true

}

在显示键盘之前,一切都很好,这些是MyCollectionView及其CollectionViewCell

框架的值

self.frame: (0.0, 75.0, 375.0, 492.0)

flowLayout.itemSize: (335.0, 492.0)

enter image description here

但是在显示键盘并调出configureCollectionView()之后,基本上只会重置flowLayout MyCollectionView的所有内容。奇怪的是,即使MyCollectionView的高度降低,控制台输出也表示实际上已增加。

self.frame: (0.0, 55.0, 375.0, 512.0)

flowLayout.itemSize: (335.0, 512.0)

这是键盘出现后的屏幕截图:

enter image description here

正如您所看到的,似乎CollectionViewCells被裁剪,并且它已经失去了它的初始alpha值。此外,在随机滚动控制台输出后:

2016-04-29 09:43:24.012 DeckWheel[15363:2028073] the behavior of the UICollectionViewFlowLayout is not defined because: 2016-04-29 09:43:24.012 DeckWheel[15363:2028073] the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values.

我知道这是由MyCollectionView变化的高度引起的,但我不知道为什么它会出错,因为在键盘显示之前一切正常。例如,我可以在单元格之间随机滚动,如果我到达最后一个单元格,它将自动创建一个没有任何崩溃的新单元格。

我已尝试MyCollectionView.invalidateLayout()然后调用configureCollectionView()重置flowLayout但似乎没有任何效果。更新MyCollectionView的正确方法是什么,以免发生这种情况?

0 个答案:

没有答案