I want to implement the UICollectionView effect with tabbar and possible to switch different content like a lot of social app does.
You can click Home
to display timeline and click Album
to display photos on content area. The top of content like avatar and username are no change, and the content offset will keep the same when you switch is completed.
I try it with a UICollectionView containing SupplementaryView for displaying avatar and handling tabbar click event. Assign different delegate, dataSource and layout object while switching different content. But I got a lot of problem and crash.
What's the correct way to implement this kind of layout and how to handle switching event?
If I understand correctly the top part of your view does not change and remains as the logo with an avatar. If this is the case I would not worry about supplementary views. Supplementary views are more for section headers/separators in a similar way to the use of section headers for UITableViews. Instead have a container view at the bottom which would hold the collectionView with UISegmentedControl above which would trigger the changes in the container view. If the container is to display the same data with different layouts (ie. photos scrolling vertically to horizontally) then the segmented control could trigger a change to the collectionViewFlowLayout but if the data changes (ie Timeline to Album) then I would suggest switching to different child view controllers in the container view triggered from the segmented control actions. You would probably want to save the current scroll offset in the container view controllers so that it can be restored when switching back. Also having separate child view controller classes will help prevent the container view controller from getting too large and keep each child collection view controller simple and focused.
Just decompose your logic, there is no necessity to keep everything in one UICollectionView
and switch data sources and delegates.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With