Can UICollectionView count horizontally? - ios

I have this from a programmatically implemented UICollectionView:
Is there a way (programmatically) to ask it to count left to right instead of top to bottom so that there would be two rows of four instead of two columns? Thank you!

If you are using a flow layout you can set the UICollectionViewFlowLayout's scrollDirection property to UICollectionViewScrollDirectionVertical:
UICollectionViewFlowLayout *layout = collectionView.layout;
layout.scrollDirection = UICollectionViewScrollDirectionVertical;

Related

iOS Swift UICollectionViewCustomLayout scrollDirection

i'm trying to change UIColloectionViewCustomLayout scrollDicretion to Horizontal. I have tried to :
let layout = CollectionViewLayout()
layout.scrollDirection = .Horizontal
self.collectionView.collectionViewLayout = layout
I need get this results
[1][4][7]
[2][5][8]
[3][6][9]
Now i have this
[1][2][3]
[4][5][6]
[7][6][9]
Here is a few links to help you understand what to do.
http://www.skeuo.com/uicollectionview-custom-layout-tutorial
https://www.amazon.com/iOS-UICollectionView-Complete-Mobile-Programming-ebook/dp/B00CFLTD50?ie=UTF8&redirect=true
On iOS, can you use UICollectionViewFlowLayout to get a vertical column layout?
UICollectionView - Horizontal scroll, horizontal layout?
Reorder cells of UICollectionView

UICollectionView first cell not aligned properly

In my app, I have a UICollectionView inside a UITableView that is showing cells to select a day.
I had a weird issue where sometimes, the cells would be centered correctly, and sometimes it wouldn't, to the point where part of the first cell is cut off in the view.
Normal
Not Aligned
What is going on? I've adjusted the UIEdgeInsets, but it doesn't seem to make an effect.
I have found out that my issue was fixed when I removed this line of code for the UICollectionViewFlowLayout of the UICollectionView
For a defined
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
Remove this
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

UICollectionViewFlowLayout minimumInteritemSpacing doesn't work

I've got two problems with my UICollectionView:
minimumInteritemSpacing doesn't work
it overflows horizontally on iOS 6
I set up the layout like this:
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.itemSize = CGSizeMake(70.0f, 70.0f);
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
layout.minimumLineSpacing = 0.0f;
layout.minimumInteritemSpacing = 0.0f;
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
// I set the size of _collectionView in layoutSubviews:
// _collectionView.frame = self.bounds;
_collectionView.contentInset = UIEdgeInsetsMake(8.0f, 8.0f, 8.0f, 8.0f);
The image shows the result on iOS 6 (on iOS 7 there is no overflow, but the spacing between columns is still not zero)
I tried this solution https://gist.github.com/OliverLetterer/5583087, but it doesn't fix anything in my case.
From the documentation for the minimumInterItemSpacing property:
For a horizontally scrolling grid, this value represents the minimum spacing between items in the same column. This spacing is used to compute how many items can fit in a single line, but after the number of items is determined, the actual spacing may possibly be adjusted upward.
The flow layout will evenly space cells across its width, with a spacing of no smaller than the minimum you set. If you don't want the spacing, you'll need to implement your own layout.
The iOS 6 overflow issue I'm not sure about. Try dropping support for iOS 6 ;)
"line spacing" can mean the space between vertical lines.
Say you have a ordinary single line horizontal collection view.
(Eg, the whole view is simply 50 high, and the items are simply 50x50.)
As user #matt has explained
a horizontal collection view has columns of cells
the "lines" as Apple means it are the vertical "lines" (!) of cells
Thus:
in the case of a simple horizontal collection view with one row,
what Apple names the "line" spacing is - indeed - the spacing between items
Thus surprisingly in a simple horizontal collection view with one row, to set the gap between items, it's just:
l.minimumLineSpacing = 6 // Apple means "vertical scan lines" by "lines"
(minimumInteritemSpacing is completely meaningless in a normal simple horizontal collection view with one row.)
This finally explains why there are 100 pages on the internet asking why minimumInteritemSpacing just doesn't work. Fantastic tip by user #matt

Can you amend UICollectionView scrollDirection programmatically?

I have a UICollectionView that is created using Storyboard (given it has quite complicated cells). I want to programmatically change the scrollDirection to be horizontal (on 4 inch screens) and vertical (on 3.5 inch screens).
I see you can set scrollDirection upon initiation, but I cannot see how you can access this property from an already created UICollectionView.
Does anyone know if this is possible?
What you are looking for is the
UICollectionViewFlowLayout
This is how you would go about using it
UICollectionViewFlowLayout *horizontalLayout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
horizontalLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
[self.collectionView setCollectionViewLayout:horizontalLayout];
I assume that your collectionView is linked with the storyboard as self.collectionView
This is the quick and dirty way. What you should do is create your own layout and subclass the UICollectionViewFlowLayout class.
An example can be found here: UICollectionViewFlowLayoutExample by AshFurrow

UICollectionView using sections leaves a gap between cells

I'm using a UICollectionView to show several sections of data. These sections have a fixed number of items. I want all the items to show in a continuous grid.
Right now I accomplish this in horizontal orientation:
But in vertical this leaves a big gap:
I want to solve this gap between sections because it's ugly and it doesn't belong there.
I'd be happy to use a custom FlowLayout, but I can't find a tutorial that points me in the right direction (I've found several, but none of them really touch this problem specifically.)
Can anybody help me solve this problem, or at least point me in the right direction?
P.S: I've implemented sections because I'm loading the data on the fly. Using 1 section isn't an option for me at this moment.
UPDATE
On request I'm adding the values used for my current FlowLayout. I'm using the standard Horizontal Flow Layout on a fullscreen (minus UINavigationBar) UICollectionView with 21 items per section.
Scroll direction: Horizontal
Cell size: 248, 196
Header / footer size: none
Min spacing for cells: 10
Min spacing for lines: 10
Section Insets: 20, 20, 10, 10
Use like this
It will solve Gap problem
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
layout.minimumInteritemSpacing = 0;
layout.minimumLineSpacing = 2;
To remove those gaps you need to create custom layout which will act as layout for your collection view. This class will child class for UICollectionViewFlowLayout.
Then you can override below two methods and can create your own custom layout as you want.
- (NSArray*)layoutAttributesForElementsInRect:(CGRect)rect
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)path
UICollectionViewLayoutAttributes is class which will deal with cell position, frame, Zindex etc
You can also use below properties.
collectionView:layout:minimumInteritemSpacingForSectionAtIndex:
collectionView:layout:minimumLineSpacingForSectionAtIndex:

Resources