UIScrollview autostart problem

I have a problem with autorun (possibly) and my scrollview!

My problem

  • I'm scrolling down View

2. Then I click on another view

3. Then I go back and scrollview looks like this and I canโ€™t scroll to the highest point (I see this in scrollview bouncing) Scrollview

Can someone help me?

+30
ios iphone autolayout ios6 uiscrollview
Sep 25 '12 at 9:50
source share
9 answers

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; } 

This reset the content to the origin, but it seems that other answers do the same.

+39
Nov 10
source share

if you are still looking for the answer I found today, after two days of sorting through the wall. I will just paste you the code, but most importantly, 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)]; } 

it all loads up to -(void)viewDidLoad

Please note that the height is 800 in both cases, which is critical to solving this problem. good luck in your project;)

+11
Nov 02
source share

I used the adam solution, but I had problems when I quit with animated: YES. In my code, the content offset is set some time after viewWillAppear (since viewWillAppear appears too early).

 - (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 to reset return it to viewDidLayoutSubviews :)

 - (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; if(!CGPointEqualToPoint(CGPointZero, self.scrollOffsetToPersist)) { self.scrollView.contentOffset = self.scrollOffsetToPersist; self.scrollOffsetToPersist = CGPointZero; } } 
+6
Jan 29 '13 at 18:45
source share

This is not very good, but I beat the automatic layout (definitely not the right way, but I was tired of trying!) By setting the size of the content in viewDidAppear after the automatic shutdown occurs, setting scrollOffset and keeping the scroll offset in viewDidDisappear, and then set the offset scroll back to this 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 it works so that I would share it.

+3
Dec 07
source share

I am using UITabBarController and various auto-layout views. Views are longer than the device screen. Switching from one tab to another sometimes leads to the problem you are describing. This only happened if the view was scrolled before the switch. I tried all the tips here, but that didn't help me. My solution was to scroll up before leaving the view. At least the work around this bug in iOS 6:

 -(void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [scrollView setContentOffset:CGPointZero animated:NO]; } 
+1
Aug 05 '13 at 22:45
source share

The problem caused that ScrollView was set by ContentOffset before applying AutoLayout. decision:

Create Private Property

 @property (assign,nonatomic) CGPoint scrollviewContentOffsetChange; 

Add code to view the 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; } 
+1
Aug 29 '13 at 10:16
source share

Have you tried this?

 self.automaticallyAdjustsScrollViewInsets = NO; 

In my case, this is exactly what solved my problem.

+1
Apr 04 '14 at 12:05 on
source share

I had the same problem. It turned out that I set a restriction on the presentation of the content, aligning its y-center with the y-center of the supervisor. When I removed this restriction, it worked fine.

0
Feb 19 '13 at 22:06 on
source share

try it

 @property (nonatomic, assign) CGPoint scrollViewContentOffsetChange; - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.scrollView.contentOffset = CGPointZero; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; self.scrollViewContentOffsetChange = _scrollView.contentOffset; } - (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; _scrollView.contentOffset = self.scrollViewContentOffsetChange; } 
0
Jul 28 '14 at 9:18
source share



All Articles