I have a problem with autolayout(maybe) and my scrollview!
My Problem
have you tried this?
self.automaticallyAdjustsScrollViewInsets = NO;
In my case this was what solved my problem.
The issue cause ScrollView was set ContentOffset before AutoLayout applied. the solution is:
Create private property
@property (assign,nonatomic) CGPoint scrollviewContentOffsetChange;
Add code to view method
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.scrollView.contentOffset = CGPointZero;
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
self.scrollviewContentOffsetChange = self.scrollView.contentOffset;
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
self.scrollView.contentOffset = self.scrollviewContentOffsetChange;
}
I had the same problem. Turned out I was setting a constraint on the content view aligning the it's y-center to the superview's y-center. When I deleted this constraint it worked just fine.
I was using adam's solution, but started to have problems when i was dismissing with animated:YES. In my code, content offset gets set a while after viewWillAppear (as viewWillAppear appears to be too soon).
- (void)viewDidDisappear:(BOOL)animated
{
self.scrollOffsetToPersist = self.scrollView.contentOffset;
self.scrollView.contentOffset = CGPointZero;
[super viewDidDisappear:animated];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSOperationQueue mainQueue] addOperationWithBlock:^
{
self.scrollView.contentOffset = self.scrollOffsetToPersist;
}];
}
EDIT: another, better way is to reset it back in viewDidLayoutSubviews :)
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
if(!CGPointEqualToPoint(CGPointZero, self.scrollOffsetToPersist))
{
self.scrollView.contentOffset = self.scrollOffsetToPersist;
self.scrollOffsetToPersist = CGPointZero;
}
}
if you are still searching for an answer i found it today after two days of headbanging the wall. I will just paste you the code, but the most important thing is when you load your scrollView..
-(void)viewWillAppear:(BOOL)animated{
[scrollView setFrame:CGRectMake(0, 0, 320, 800)];
}
-(void)viewDidAppear:(BOOL)animated
{
[scrollView setScrollEnabled:YES];
[scrollView setContentSize:CGSizeMake(320, 800)];
}
all this is loaded before -(void)viewDidLoad
notice the height is in both instances 800, which is crucial for resolving this problem. good luck with your project ;)
I use an UITabBarController and different views with auto layout. The views are longer than the screen of the device. Switching from one tab to the other lead sometimes to the problem you describe. This only happened if the view has been scrolled down before the switch. I tried all the advice here but this did not work for my case. My solution was to scroll up again before leaving the view. At least a work around for this bug in iOS 6:
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[scrollView setContentOffset:CGPointZero animated:NO];
}