James
James

Reputation: 3805

UIScrollView doesn't scroll on iPad but works on iPhone

UPDATE

I know turning off AutoLayout fixes the scrolling issue. However, I want to use AutoLayout still. Why does it work on iPhone and not iPad? Is there a work-around to this? This seems like a bug...


I've tried several things to get this to work and nothing seems to do it for me. I have a function that I call in viewDidAppear that resizes my UIScrollView's content size. My view height is 916 and the log is saying it's resizing the content height to 1267. Scrolling is enabled (I have it set in the storyboard but also set it in code to try to get this to work).

Here's the odd thing. If I click inside of a text field, scrolling suddenly works. I do alter the scroll view's content size during that time, but anything I do there I have tried doing within my resize function and it still did not work. The resize function is below as well as the animation for when I click inside a text view.

- (void)resize_scrollview_to_fit {
    CGFloat scrollViewHeight = 0.0f;
    for (UIView *view in scrollView.subviews) {
        if (view.frame.size.height + view.frame.origin.y > scrollViewHeight) {
            scrollViewHeight = view.frame.size.height + view.frame.origin.y + 10;

            NSLog(@"Inside Scroll View 'If' Statement");
        }
    }

    NSLog(@"Resizing scroll view to fit: %f", scrollViewHeight);

    [scrollView setContentSize:(CGSizeMake(scrollView.frame.size.width, scrollViewHeight))];

    NSLog(@"Scrollview content height: %f", scrollView.contentSize.height);

    NSLog(@"Screen height: %f", [[UIScreen mainScreen] bounds].size.height);
}

The log I get for this each time it is ran is:

2014-06-26 14:14:05.455 eTicket[1113:60b] Inside Scroll View 'If' Statement
2014-06-26 14:14:05.456 eTicket[1113:60b] Resizing scroll view to fit: 1267.000000
2014-06-26 14:14:05.457 eTicket[1113:60b] Scrollview content height: 1267.000000
2014-06-26 14:14:05.458 eTicket[1113:60b] Screen height: 1024.000000

- (void) animateTextView:(UITextView *) textView up: (BOOL) up
{
    const int movementDistance = keyboardSize.height - 58; // tweak as needed
    //const float movementDuration = 0.3f; // tweak as needed

    NSLog(@"Moving View Up: %f", keyboardSize.height);

    int movement = (up ? -movementDistance : movementDistance);

    scrollView.frame = CGRectMake(scrollView.frame.origin.x, scrollView.frame.origin.y, scrollView.frame.size.width, scrollView.frame.size.height + movement);

    [self resize_scrollview_to_fit];

    if (up && textView.frame.origin.y + 10 < scrollView.contentSize.height - scrollView.frame.size.height) {
        scrollView.contentOffset = CGPointMake(0, textView.frame.origin.y - 10);
    }else if (up) {
        scrollView.contentOffset = CGPointMake(0, scrollView.contentSize.height - scrollView.frame.size.height);
    }else{

    }
}

I have placed the resize_scrollview_to_fit method call in several places within the code with no change in functionality. Any help would be greatly appreciated.

Thanks, James

Upvotes: 0

Views: 641

Answers (1)

Losiowaty
Losiowaty

Reputation: 8006

As stated in my comments under the question :

When using AutoLayout, you should set contentSize of UIScrollView in viewDidLayoutSubviews - after that method you can be fairly sure that autolayout won't change anything. Refer to accepted answer here for some more explanation about viewDidLayoutSubviews

Upvotes: 3

Related Questions