Can you amend UICollectionView scrollDirection programmatically? - ios

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

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;

Change scroll direction in UICollectionView

I have a UICollectionView which I have setup, everything works fine (selection, headers, etc), however, I want to change the scroll direction in some situations.
In short if I go into the story board and change the scrollDirection it works fine but I want to do it programatically!
I have tried to change the scroll direction of the collection view directly with something like
[myCollectionView setScrollDirection:....and so on.......
But this does not work, I can not find scrollDirection or similar in there.
I have also tried to setup a flow layout but I am sure i am doing this wrong (i.e. trying to set a ViewLayout to a FlowLayout).
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
[myCollectionView setCollectionViewLayout:flowLayout];
This crashes with a lot of Constraint problems and I suspect I need to do a lot more work with the flowLayout (from what I have found it is a bit above me right now).
It should also be noted that I am using a custom cell, headers and footers.
In short is there an easy way to do this or not? OR does anyone know a good Flow Layout tutorial?
EDIT
I setup the collection view as such;
[myCollectionView setDataSource:self];
[myCollectionView setDelegate:self];
I implement these delegate methods and all work fine
viewForSupplementaryElementOfKind
numberOfSectionsInCollectionView
numberOfItemsInSection
cellForItemAtIndexPath
didSelectItemAtIndexPath
I have added the DataSource and Delegate to the .h and also the FlowLayout delegate BUT I am not sure what I should also have for the latter.
Most of the visual layout is done in Story Board, there are a few things such as Font, size and colour which I do programatically.
ANOTHER EDIT
This is the error when I try to change the FlowLayout, I also get this when I try and invalidate the layout.
Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
"<NSAutoresizingMaskLayoutConstraint:0x89967f0 h=--& v=--& V:[menuCell:0x8991700(50)]>",
"<NSLayoutConstraint:0x8991200 menuCell:0x8991700.bottom == UILabel:0x8992be0.bottom + 100>",
"<NSLayoutConstraint:0x898fd50 UILabel:0x8992be0.top == menuCell:0x8991700.top + 3>"
Will attempt to recover by breaking constraint
Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful.
do this:
- (IBAction)changeDirection:(UIButton *)sender
{
UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)[self.collectionView collectionViewLayout];
if(layout.scrollDirection == UICollectionViewScrollDirectionHorizontal)
{
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
}
else
{
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
}
}
It works for me.
In swift you can do this:
//From the collection view subclass
if let layout: UICollectionViewFlowLayout = self.collectionViewLayout as? UICollectionViewFlowLayout {
layout.scrollDirection = .Vertical
}
Ok, try calling invalidateLayout on your collection view like so:
[myCollectionView.collectionViewLayout invalidateLayout];
This forces the collection view to update its layout at that point (See apple documentation here). I'm not certain, but I don't imagine this is called when you change the scroll direction.
See if that gets you anywhere near!
You can do it with property observer didSet{} with your collection view outlet.
Assuming that the outlet for the collectionView is myCollectionView, in the code add a didSet property observer to the outlet and change the layout's direction. Something like-
#IBOutlet weak var myCollectionView:UICollectionView!{
didSet{
let layout = contentCollectionView.collectionViewLayout as!
UICollectionViewFlowLayout
layout.scrollDirection = .Vertical
}
}
Normally a collectionView is dropped in storyboard from object library, it automatically comes with FlowLayout. You can change this layout's flow direction by telling your code that when I get my collection view, I want its layout's scroll direction to be horizontal or vertical.

Can UICollectionView count horizontally?

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;

UICollectionView cell disappears

I am using collectionView in my iPad application. I have used custom layout class which inherited UICollectionViewFlowLayout class.
I am using horizontal scroll directional. The problem is, whenever we use scroll for collection view, some times cell get disappear. I debugged the code, and found that in data source method after creating cell, its hidden property get automatically ON.
I tried using below line but its not working
cell.hidden = NO
I have below method also to invalidate layout on bound change.
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
return YES;
}
But still I am facing problem. Any solution on this ?
Thanks in advance.
There is a known issue which may cause cells to disappear in UICollectionView. If their fix doesn't work for you, try PSTCollectionView,
which is an open source replacement for UICollectionView. If PSTCollectionView works, you should file a radar ticket to Apple.
In my project, the UICollectionViewCell disappear also ,the flowLayout i use is a custom layout named UICollectionViewLeftAlignedLayout . Its scroll direction is horizontal .I debug the project again and again, have tried every solution i can find through google.com. All that did not work for me.
At the end ,i delete the UICollectionViewLeftAlignedLayout and use native
UICollectionViewFlowLayout through:
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
[layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
_collectionView.collectionViewLayout = layout;
_CollectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
And I set the cell spacing through storyboard(also you can set them through the layout's propery):
Then i run the project again , the UICollectionViewCell DO NOT DISAPPEAR anymore. Hope my case can help you .

Resources