Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue when switching orientation #42

Open
HassanTaleb90 opened this issue Apr 19, 2019 · 3 comments
Open

Issue when switching orientation #42

HassanTaleb90 opened this issue Apr 19, 2019 · 3 comments

Comments

@HassanTaleb90
Copy link

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
Copy link
Owner

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
Copy link
Author

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

@MaherKSantina
Copy link
Owner

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants