1

So the basic issue is that when I click the cell, it should go to cell index 1 in the new view controller, but when you have autolayout on. The collectionview content offset change goes away and it's reset. Turn it off, works fine. Autolayout is somehow causing content offset to reset but I'm not sure why or how to resolve this.

Code available here.

https://github.com/HaloZero/AutolayoutCollectionViewIssue

2
  • If you call your snapToCellAtIndex:withAnimation:method from viewDidAppear instead of viewWillAppear, it works. However, you do see the first cell briefly before the second one appears. Commented Jun 5, 2013 at 5:54
  • hello @HaloZero where are you ... I have solved your problem please test at your side. Commented Jun 6, 2013 at 4:14

2 Answers 2

1

use you code as :--

- (void) viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self performSelectorOnMainThread:@selector(doyouwork) withObject:nil waitUntilDone:NO]; } -(void)doyouwork { [self snapToCellAtIndex:1 withAnimation:NO]; } - (void) snapToCellAtIndex:(NSInteger)index withAnimation:(BOOL) animated { NSIndexPath *path = [NSIndexPath indexPathForRow:index inSection:0]; [self.collectionView scrollToItemAtIndexPath:path atScrollPosition:UICollectionViewScrollPositionLeft animated:animated]; } 

working for my side.

Sign up to request clarification or add additional context in comments.

1 Comment

so does view WillAppear not happen on the main thread or something? Sorry about the delayed response, I took this into Apple guys at WWDC and they mentioned that I should file this as a bug.
0

It is not that auto-layout is resetting the content offset, but the subviews have not been positioned when you try to snap the cell.

With auto-layout, the subviews are positioned a bit later (in which the viewWillAppear is called first). Try to use viewDidLayoutSubviews instead for your snapping of the cell:

From the documentation for viewDidLayoutSubviews:

Notifies the view controller that its view just laid out its subviews.

So after all the constraints have been calculated and your subviews have been laid, you can call the snapping method.

- (void)viewDidLayoutSubviews { [super viewWillLayoutSubviews]; [self snapToCellAtIndex:1 withAnimation:NO]; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.