通过修改约束来调整大小时的UICollectionView itemSize问题

时间:2015-04-07 16:13:24

标签: ios swift uicollectionview

我有一个水平滚动UICollectionView,它位于垂直滚动UICollectionView的单元格内。当垂直滚动collectionView滚动时,其内部的水平滚动collectionView会根据它在垂直集合视图中呈现单元格的位置来调整大小。

collectionViews概要:

layout:                   scrolling:
______________________    ______________________
|         ^          |    | while   ^          |
|         | Vertical |    |   this  | scrolls: |
| __________________ |    | __________________ |
| |                | |    | |       ^        | |
| |                | |    | |       |        | |
| |<- Horizontal ->| |    | |<- This Grows ->| |
| |                | |    | |________________| |
| |________________| |    |____________________|
|____________________|    |   ______________   |
                          |   |     ^      |   |
                          |   |     |      |   |
                          |   |<-  grows ->|   |
                          |   |____________|   |
                          |____________________|

要在滚动时更改大小,我会为更新框架的内部collectionView更新宽度NSLayoutConstraint(collectionView有其他约束以保持其纵横比和位置正确):

for (constraint, definition) in parralaxDict {
    // calculates offset for each constraint
    constraint.constant = definition.parralaxOffset(forPosition: position, inRect: bounds)
}
layoutIfNeeded()

这很棒,但是我得到了关于流程布局的 TON 错误的itemSize:

the behavior of the UICollectionViewFlowLayout is not defined because: 
the item height must be less than the height of the UICollectionView minus the section insets top and bottom values.

没问题,我想。如果需要,我会在调用布局之前使布局无效:

for (constraint, definition) in parralaxDict {
    // calculates offset for each constraint
    constraint.constant = definition.parralaxOffset(forPosition: position, inRect: bounds)
}
// layout.invalidateLayout() // tried here, nope
layoutIfNeeded()
// layout.invalidateLayout() // tried here, double nope

由于这不起作用,我尝试将流程布局shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool设置为true。这做了两件事:

  1. 仅对FULL更改触发(即当CGFloat命中1.0,2.0,3.0但不是1.1112,1.1345等时触发。)
  2. 没有解决问题。
  3. 我还尝试在更新约束时无法直接设置itemSize。

    值得注意的是,我做了UICollectionViewFlowLayout的子类,如下所示:

    class PhotoCollectionViewFlowLayout: UICollectionViewFlowLayout {
    
        class func layout() -> PhotoCollectionViewFlowLayout {
            let layout = PhotoCollectionViewFlowLayout()
            layout.setup()
            return layout
        }
    
        private func setup() {
            scrollDirection = .Horizontal
            minimumInteritemSpacing = 0.0
            minimumLineSpacing = 0.0
            sectionInset = UIEdgeInsetsZero
        }
    
        override func prepareLayout() {
            super.prepareLayout()
    
            if let theCollectionView = collectionView {
                var newItemSize = itemSize
                newItemSize.width = theCollectionView.bounds.size.width - (sectionInset.left + sectionInset.right)
                newItemSize.height = theCollectionView.bounds.size.height - (sectionInset.top + sectionInset.bottom)
                itemSize = newItemSize
            }
        }   
    }
    

    有什么想法我可以做些什么来消除这些错误?

0 个答案:

没有答案