gpichler
gpichler

Reputation: 2381

Can't scroll back to top, if I switch between UIViewController with UITabBar

I have an issue with my UIScrollView, if i click on a tab bar item to switch to an other ViewController and then go back to the ViewController with the UIScrollView i can't scroll back to the top. I have to switch again between ViewControllers and then the content in the UIScrollView is correctly shown. This procedure is really uncomfortable for the user. This is my code:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self becomeFirstResponder];  // View as first responder

    scrollView.scrollEnabled = YES;
    [scrollView setUserInteractionEnabled:YES];
    testLabel.text = @"there is a long, long text normally";   
}

I didn't set the contentSize on purpose, because the UIScrollView automatically gets the correct height depending on this label. My Problem is if i scroll down and then switch to an other ViewController, when I'm coming back again to this screen I can't scroll up to the top anymore.

Some further information: Auto Layout used, Hierarchy is: ViewController - View - UIScrollView - Label, ...

Upvotes: 1

Views: 1236

Answers (2)

user1105951
user1105951

Reputation: 2287

I'm not sure if this is the case, but don't forget that on the iphone (and not ipad) :

You need to make sure, that if you got uiscrollview(tableview, uiwebview, uicollectionview), this scroll view -is the only- scroll view on the screen that returns scrollTopTop = YES.

Set all the other to scrollTopTop = NO.

Upvotes: 0

Corey
Corey

Reputation: 590

I've been struggling with this exact same problem! Unfortunately I haven't been able to figure out exactly what's going on or causing it (bug on Apple's side?), but I have created a work around that seems fairly effective.

What I'm doing is in the viewWillDissapear method I'm saving the scrollview bounds height position in a local float variable, and then completely resetting the scrollview to the top.

Then in the didLayoutSubviews method, I scroll back to the saved position.

Here's my code.

Setup variable for storing last scroll value and lazily instantiate:

@property (nonatomic) float lastScrollHeight;

@synthesize lastScrollHeight = _lastScrollHeight;

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

After the view did layout subviews, scroll to the saved value (will be zero first time):

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

Every time the view will disappear, fully "reset" the scrollview:

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

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

This is my first time answering a question on Stack Overflow! If you found my answer useful please hit the gray up arrow on the left.

Upvotes: 6

Related Questions