UIScrollview Autolayout Issue
Solution 1
The following code snippet in the containing view controller also seems to solve the problem, without relying on explicit sizes:
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
self.mainScrollView.contentOffset = CGPointZero;
}
It does reset the content offset to the origin, but it seems that so do the other answers.
Solution 2
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 ;)
Solution 3
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;
}
}
Solution 4
This isn't great but I beat auto-layout (definitely not the correct way but I was sick of trying!) by setting the content size in viewDidAppear after autolayout happens, setting the scrollOffset and persisting the scroll offset in viewDidDisappear, and then setting the scroll offset back to it's persisted state in viewDidAppear.
Like this:
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:YES];
self.scrollView.contentSize = self.scrollViewInnerView.frame.size;
self.scrollView.contentOffset = [self.scrollOffsetToPersist CGPointValue];
}
-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:YES];
self.scrollOffsetToPersist = [NSValue valueWithCGPoint:self.scrollView.contentOffset];
self.scrollView.contentOffset = CGPointZero;
}
Not at all elegant, but works so thought I'd share.
Solution 5
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;
}
Mathias Aichinger
Updated on June 07, 2022Comments
-
Mathias Aichinger almost 2 years
I have a problem with autolayout(maybe) and my scrollview!
My Problem
- I scroll down
2.Then I push to another View
3.Then I go back and the scrollview looks like that and I'm not able to scroll to the highest point.(I see it in the bouncing of the scrollview)
Can anybody help me?
-
iMeMyself over 11 yearsdid you set the frame size correctly ?
-
iMeMyself over 11 yearsif you are coding scrollview programmatically without nib, add the code chunk to the question .. this can help you get better answers
-
Mathias Aichinger over 11 yearsI'm coding nothing! everything was set in the interfacebuilder
-
iMeMyself over 11 yearsset frame of scroll view like this CGRect scrollFrame = CGRrectMake(x, y, width , height ); ScrollView.frame = scrollFrame;
-
chebur over 10 yearsFixed this issue at the related thread stackoverflow.com/a/18475112/318790
-
Mathias Aichinger over 11 yearsthis works! but when i set it back to the right value(in viewdidappear) it is flickering! so I continue looking for a better way....
-
Mathias Aichinger over 11 yearsset right offset in viewDidLayoutSubviews back and everything is fine :)
-
Mathias Aichinger over 11 yearsapple wrote the same thing... probably the best solution so far... but I think viewDidLayoutSubviews is the better position to set the offset back
-
Ethan Mick about 11 yearsThis is an unbelievably and frustrating bug/issue. People aren't prepared to deal with this bug when setting things up in Interface Builder.
-
Michael about 11 yearsYep this is what worked for me. A simple idea such as resizing subviews in a scroll view, the execution is rather complex... Spent hours messing around with UIScrollView and autolayout before finally settling on this.
-
DesignatedNerd almost 11 yearsNote that if you set it in viewDidLayoutSubviews, you may need to also save the offset in viewDidDisappear: or scrollview offset will jump to zero when you present a modal view controller and won't reset properly. I filed a bug with Apple on this behavior as well: openradar.appspot.com/radar?id=3011407
-
Yannick over 10 yearsI had to use this fix also, but before I presented my UIViewController I let the scrollView scroll to the top. My scrollView was off-screen so I had no flickering. But you could create a method to reset the scrollview to its origin point and call it after 1 second or so (performSelector:withObject:afterDelay:); when you start presenting your UIViewController.
-
Mathias Aichinger over 10 yearsLooks like that Apple has fixed this issue in the newest iOS! :)
-
Mathias Aichinger about 10 yearsthis was an iOS 6 issue. in iOS 7 everything is fine!
-
Frade over 9 years@matchi1992 I have the most recent version, and I'm facing this issue. Can someone tell why this happen? please.
-
Beto about 9 years@Frade do you found the workaround? I have Xcode 6.2
-
Frade about 9 years@Beto Bens, not exactly a workaround. I redesigned the the interface with new/different auto-layout constraints. First I had lots of uibuttons inside a single view, and I was trying to place the view in the scrollView. I took out the view, so i placed all buttons and views directly on scrollView, I fixed one relative to scrollView, and the others relative to the first.. And it worked.
-
Ilesh P over 8 yearsI solve my problem using
NSoperationQueue
putting in theviewDidLayoutSubviews
method and setcontectsize
of scrollview.Thanks @kukosk -
kemdo over 7 yearsthis problem come back in ios9