Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 601
Recycled Steel Avatar asked Jan 13 '14 16:01

Recycled Steel


1 Answers

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.

like image 160
Pratham Mehta Avatar answered Sep 23 '22 19:09

Pratham Mehta