Sp3kk
Sp3kk

Reputation: 79

UIScrollview jumps off screen when switching between views

I have a Tab-bar app with a scroller view in the first tab and some other stuff in the second one. The scrolling works perfectly fine, but if I scroll down and then switch to the second tab and then back again the scrollview is jumped so far up that the whole thing is out of frame and I can barley scroll up and reach half way back to the top. If I go back to the second tab and then back to the tab with the scroll view again it is back in its spot just fine, but every time I switch for the first time I run the app it just flies far off the screen.

So I added a method that I thought would fix my problem, and it looks like this:@property (nonatomic) float lastScrollHeight;

@synthesize lastScrollHeight = _lastScrollHeight;

-(float)lastScrollHeight{
    if(!_lastScrollHeight) _lastScrollHeight = 0;
    return _lastScrollHeight;
}



 -(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    [self.scroll setContentOffset:CGPointMake(0.0f, self.lastScrollHeight) animated:NO];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.lastScrollHeight = self.scroll.bounds.origin.y;
    [self resetApplesBrokenScrollview];
}

-(void)resetApplesBrokenScrollview{
    [self.scroll setContentOffset:CGPointZero];
    CGRect sFrame = self.scroll.bounds;
    sFrame.origin = CGPointZero;
    self.content.bounds = sFrame;
    CGRect cFrame = self.scroll.frame;
    cFrame.origin = CGPointZero;
    self.content.frame = cFrame;
}

And inside the viewDidLoad method :

[super viewDidLoad];
[scroll setScrollEnabled:YES];
[scroll setContentSize:CGSizeMake(320, 1900)];
scroll.contentInset=UIEdgeInsetsMake(0.0,0.0,1919.0,0.0);

And I'm pretty confident about this code, but something seams to be wrong... Any help would be appreciated. thx :)

Upvotes: 3

Views: 403

Answers (1)

ClemensL
ClemensL

Reputation: 414

Even if you have a long list, thats why you set your contentsize. This immense bottom inset makes no sense cause it is way out of your screenSize.height.

If you have f.i. a view overlapping your scrollView then you need to provide a contentInset with the height of your overlapping view (just an example).

Your scrollView frame should be the size you want your content to scroll in. The contentSize is the actual size the content takes/needs.

Upvotes: 1

Related Questions