iOS: CollectionView stick header while scrolling - ios

I am implementing CollectionView with custom layout. It have vertical scrolling.
I want to make that some cells will be sticked to the top while scrolling like section header in tableView.
The idea is to create day collectionView with time. And while user will be scroll thought time the current hour will be sticked to the top(if it will be not visible and hidden by top). It will be only if the current hour hidden by top, not by bottom. And the other items should be scrollable like usual.
I think that I should not use supplementaryViews here
Is it possible and how can I do it?

You need to override shouldInvalidateLayoutForBoundsChange so it returns YES. This means layoutAttributesForSupplementaryViewOfKind and layoutAttributesForElementsInRect are called when the UICollectionView is scrolled, and in there you can then calculate proper frames for your views, including the sticky views. If you have a complicated UICollectionView, this may lead to bad performance, but as far as I know it's the easiest way to do this for now. Your other bet is looking at invalidation contexts, but they're quite badly documented. If you still want to experiment with those, you could take a look at Using Invalidation Contexts for UICollectionViewLayout

Take a look at UICollectionView's supplementaryView.
Apple's Collection View Programming Guide

Related

How to align UICollectionViewCells from left to right?

I should design UICollectionView like below image.
It was almost done. but my UICollectionView is displaying like below image.
I thought it would be solved if I set semanticContentAttribute of UICollectionView to forceLeftToRight. but the result was not changed.
How can I design that like first image?
forceLeftToRight simply forces a left to right layout even when the locale is a right to left locale, so this doesn't help since UICollectionViewFlowLayout centers the cells within its view.
The complicated way to do what you want is to subclass UICollectionViewFlowLayout and override layoutElementsForItem(at:) to place the last cell on the left.
The simpler way is to add an additional invisble/empty cell in the case where there is an odd number of cells.

Adding subview to UICollectionView - disables scrolling

I want to add a subview to a UICollectionView in order to make a left panel that scrolls with the collectionview.
Using
[self.collectionView addSubview:myView]
all touches become disabled and I can no longer scroll the view. I've read that adding a subview to a collectionView like this is bad practice.. is this true? Why does it disable touches from reaching the collectionView event when
userInteractionEnabled = NO
I'm trying to do this: imgur link by grabbing the frame position of the first cell in each section, and adding a dot with to myView with the same y value.
Thanks for any help!
Adding subviews using the addSubview: method to a UICollectionView is very bad practice. It can cause a lot of different problems in the normal behaviour of the CollectionView. It can obstruct the views underneath it, capture the touch events, preventing them from reaching the actual scrollView inside the CollectionView, etc. The subviews added using this method will also not scroll as the other elements in the CollectionView do.
The correct way to do what you want is to implement a new type of UICollectionViewCell for the dots, and compute their locations in the prepareForLayout and layoutAttributesForElementsInRect: methods. Basically you'll have either one or two cells in each row. Which ones will have two rows will be determined by you in the methods I've mentioned.
In fact, Apple's docs have a perfect example that's even more complex than what you're trying you achieve. You should check it out from this link.
May I know the purpose of that scroll view ? Because, if you're looking for a subview that displays only a label or image etc., You can use custom collectionview cell instead if I am not wrong... Hope it helps :) :)

Choppy/Laggy scrolling of Collection View with Dynamic Content

I am having a problem where my collection view scrolling becomes very choppy/laggy as I get more and more cells. The screen contains a list of messages and comments for the messages, so every single view is dynamically sized. I have a bunch of custom layout attributes which I apply to handle laying and sizing everything.
I apply the custom attributes to the cells by overriding applyLayoutAttributes: and then calculate all the subviews frames from those.
I already cache the calculated sizes of cells but it is not enough.
I assume that there are some techniques people use to address this problem, like caching layout attributes, or somehow preventing the need to layout a cells subviews everytime cellForRow is called?
I assume this is a common problem since it is a problem I have faced with literally every table I make with dynamic content? I would appreciate any ideas :)
P.S. Rasterizing the cells doesn't help.

Customizing / subclassing UICollectionViewLayout to enable scrolling in multiple directions

I'm designing a layout for my app that is going to take advantage of UICollectionView. I have created some moderately complex collectionView-based apps before, but I may need to subclass the layout class for this one. The main point I'm trying to figure out is whether I can have different sections of the same collectionView scroll in different directions.
Based on what I have read and tried so far, the only way to have one section scroll horizontally and another vertically would be by using multiple nested instances of UICollectionView.
This is a basic idea of my layout. My guess is that even with subclassing, a single UICollectionView would not be able to handle it. Or would it?
I think you could do it with a custom UICollectionViewLayout but I'd use the default flow layout with one cell containing something like a SwipeView for the section 0 and other cells in your section 1.

UICollectionView scroll single column

I have a problem, it is not so much about a bug, but rather how to approach the issue.
I wanna create a UICollectionViewLayout where I can scroll a single column. Normally when
we scroll a scroll view, all the subviews scroll up/down/left/right. But here I wanna have a set of columns, and basically as user scrolls vertically, I want to only scroll that column and leave the others where they were. More over if the user scrolls horizontally I want the normal behavior, that is, all columns scroll left or right.
Any idea how to approach this problem ?
It's a little late, but, better late than ever!
You are right in that you need to rock your own UICollectionViewLayout. I recommend you start with having it work normally before you start working on your particular behaviour.
Basically what you want to do is have all cells but one column be vertically sticky.
To achieve that, you need reposition all your cells (but the ones that actually move) based on the Collection View's xOffset and yOffset.
Checkout my answer on this post for more information: https://stackoverflow.com/a/20987008/322377
For this you can use a UITableView with custom UITableViewCell containing UIScrollView and you can set scrollview to scroll in desired direction.

Resources