iOS 6.0: UICollectionView doesn't respect clipsToBounds with pagingEnabled

后端 未结 4 1282
太阳男子
太阳男子 2021-02-06 07:37

Background

I am implementing a UICollectionView (for the first time) in an effort to achieve a paged horizontal scroll view of tiles. I\'d

相关标签:
4条回答
  • 2021-02-06 08:27

    Many thanks for the tip about using a negative minimumLineSpacing. I created a tester application which uses a collection view cell loaded from a xib file. The cell has a transparent background and an “inner” view for the cell's content.

    In this way, a custom flow layout is not necessary.

    https://github.com/j4johnfox/CollectionViewTester

    0 讨论(0)
  • 2021-02-06 08:29

    I'm not an expert in collectionView, but it could be possibly do with this line in cellForItemAtIndexPath:

    [cell.contentView addSubview:label];
    

    Everytime it's called, another label subview is added to cell. Either check for an existing label or subclass UICollectionViewCell?

    0 讨论(0)
  • 2021-02-06 08:29

    You'll want to also override -pointInside:withEvent: to allow scroll gestures to start outside the frame of the collection view. I do this using a UIEdgeInsets property in my collection view subclass:

    - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
        CGRect extendedBounds = UIEdgeInsetsInsetRect(self.bounds, self.touchAreaInsets);
    
        return CGRectContainsPoint(extendedBounds, point);
    }
    

    If you don't need App Store safety, you can override _visibleBounds to avoid negative spacing hacks:

    - (CGRect)_visibleBounds {
         return UIEdgeInsetsInsetRect(self.bounds, self.touchAreaInsets);
    }
    

    If you're not too pressed on code size and need App Store safety you could also subclass PSTCollectionView and possibly override visibleBoundRects for the same effect.

    0 讨论(0)
  • 2021-02-06 08:42

    Turns out the solution to this was actually quite simple. I just needed to overlap the UICollectionViewCell cells by enough pixels to have them still show within the collectionView's frame after the paged scrolling finishes. The relevent code was

    layout.itemSize = CGSizeMake(300, 300);
    layout.minimumLineSpacing = -20.0;
    

    And I subclassed the UICollectionViewFlowLayout and overrode the (CGSize)collectionViewContentSize method to return the non-overlapped size of the cells.

    0 讨论(0)
提交回复
热议问题