Reproducing Popular iOS Controls - Part 9: | Ray Wenderlich Videos

In this challenge you'll enhance the control by letting the user tap on a cell to select it.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/5298-reproducing-popular-ios-controls/lessons/9

There is a Bug in collection view layout.
We can’t select the first and second images, It suddenly jumps to another one!

@leamars Can you please help with this when you get a chance? Thank you - much appreciated! :]

@leamars @shogunkaramazov
I’ve found a solution for this using UICollectionViewDelegateFlowLayout,

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    let side = lensCollectionView.frame.height  * 2
    return UIEdgeInsets(top: 0, left: side, bottom: 0, right: side)
}

fixed it , *2 is because we the collection view end with 2 cells at start and end of it self. but scroll view can’t extend it to center!
It helps left and right of section ( we have 1 section here at all ) to have necessary margin ( here is UIEdgeInsets )

1 Like

Fantastic! Great job on figuring that out on your own :D!

Thanks for posting your solution, and apologies for not replying sooner.

I just wanted to note that to have it perfectly centered, I used :
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { let inset = lensCollectionView.frame.height * 0.9 * 2 + lensCollectionView.frame.height * 0.1 return UIEdgeInsets(top: 0, left: inset, bottom: 0, right: inset) }

And in LensFlowLayout, in override func prepare()method, we should call super.prepare at the end to avoid a “UICollectionViewFlowLayout undefined behavior” warning in the console.

1 Like

Fantastic :D! Thanks for sharing!

your solution doesn’t work properly. Still first and last items not getting centered.

Sorry, I realized that I only tested it on iPhone X, I hope you found a better solution.

Thanks for the solution. It fix the drag problem. However when you select the 2nd icon, it will trigger viewDidLayoutSubviews and scroll back to middle icon.

My solution is add a property to lensController to determine whether it is the first time to layout subviews. It works though I don’t know if there is a better solution to address the multiple trigger of viewDidLayoutSubviews problem.

@bing Thank you for sharing your solution - much appreciated!