MaherKSantina / MSPeekCollectionViewDelegateImplementation

A custom paging behavior that peeks the previous and next items in a collection view
https://medium.com/@maher.santina90/how-to-create-cells-that-peek-on-the-sides-like-ios-11-app-store-ef4bb54c0c7d
MIT License
356 stars 32 forks source link

Issue when switching orientation #42

Open HassanTaleb90 opened 5 years ago

HassanTaleb90 commented 5 years ago

Hey, there's an issue when switching orientation. When I tried to select programmatically a cell, the selected cell is scrolled weirdly Here is my codes: var selectedItemIndex = 0

func selectAnItem(){
        let itemContentOffset = peekImplementation.scrollView(collectionView, contentOffsetForItemAtIndex: selectedItemIndex)
        collectionView.setContentOffset(CGPoint(x: itemContentOffset, y: 0), animated: false)
    }

override func viewWillLayoutSubviews() {
        selectAnItem()
    }
func peekImplementation(_ peekImplementation: MSPeekCollectionViewDelegateImplementation, didChangeActiveIndexTo activeIndex: Int) {
    print("Changed active index to \(activeIndex)")
    selectedItemIndex = activeIndex
}

func peekImplementation(_ peekImplementation: MSPeekCollectionViewDelegateImplementation, didSelectItemAt indexPath: IndexPath) {
    print("Selected item at \(indexPath)")
}

a Gif: ezgif-5-098a5e011014

For users having this issue, you can do this:

override func draw(_ rect: CGRect) {
        super.draw(rect)
        self.collectionView.collectionViewLayout.invalidateLayout()
        self.selectAnItem()
    }
MaherKSantina commented 5 years ago

Thanks @HassanTaleb90 for raising this issue! Are you using the collection view inside another view or cell or its just in the view controller?

HassanTaleb90 commented 5 years ago

Welcome @MaherKSantina . I used it in a table view cell

MaherKSantina commented 5 years ago

Ohh okay nice! Thanks for sharing your solution with me! Do you think you can make a pull request on the documentation (Readme.md) to add this information so that others don't experience the same problem?