UIScrollView不在iPad上滚动,但适用于iPhone

时间:2014-06-26 16:06:14

标签: ios ipad uiscrollview

更新

我知道关闭AutoLayout修复了滚动问题。但是,我还想使用AutoLayout。为什么它适用于iPhone而不是iPad?这有解决方法吗?这似乎是一个错误......


我已经尝试了几件事来让它发挥作用,似乎没有任何事情可以帮助我。我在viewDidAppear中调用了一个函数来调整UIScrollView的内容大小。我的视图高度是916,并且日志说它正在将内容高度调整为1267.滚动已启用(我已将其设置在故事板中,但也将其设置为代码以尝试使其工作)。

这是奇怪的事情。如果我在文本字段内单击,则突然滚动工作。我确实在那段时间内改变了滚动视图的内容大小,但是我在那里做的任何事情我都尝试在我的调整大小功能中进行,但它仍然无法正常工作。调整大小功能以及我在文本视图中单击时的动画。

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

每次运行时我得到的日志是:

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{

    }
}

我已将resize_scrollview_to_fit方法调用放在代码中的多个位置,而不会改变功能。任何帮助将不胜感激。

谢谢, 詹姆斯

1 个答案:

答案 0 :(得分:2)

正如我在问题的评论中所述:

使用AutoLayout时,您应该在contentSize中设置UIScrollView的viewDidLayoutSubviews - 在该方法之后,您可以相当确定自动布局不会改变任何内容。有关viewDidLayoutSubviews

的更多说明,请参阅accepted answer here