以编程方式滚动底部的滚动视图 - iphone

时间:2011-01-13 19:28:27

标签: iphone uiscrollview

我在scrollview中动态添加一些视图并增加了scrollview的内容大小,但我想在其高度底部滚动

scrollRectToVisible 对我没有帮助。它只是滚动到我的iPhone屏幕的可见视图,但我想达到scrollview内容的底部。

有人能给我一些示例代码吗?

谢谢,
Naveed Butt

5 个答案:

答案 0 :(得分:5)

我稍微修改了@jer的解决方案:

if([yourScrollView contentSize].height > yourScrollView.frame.size.height)
{
  CGPoint bottomOffset = CGPointMake(0, [yourScrollView contentSize].height - yourScrollView.frame.size.height);
  [yourScrollView setContentOffset:bottomOffset animated:YES];
}

这会将内容滚动到UIScrollView的底部,但只有在需要完成时才会(否则会产生奇怪的上/下跳跃效果)

此外,我注意到如果你不从内容的高度减去scrollview本身的高度,它会将内容向上滚动到可见的位置。

答案 1 :(得分:3)

使用类似的东西:

CGPoint bottomOffset = CGPointMake(0, [yourScrollView contentSize].height);
[yourScrollView setContentOffset:bottomOffset animated:YES];

如果您不想将其设为动画,只需将YES更改为NO

答案 2 :(得分:0)

CGFloat yOffset = scrollView.contentOffset.y;

CGFloat height = scrollView.frame.size.height;

CGFloat contentHeight = scrollView.contentSize.height;

CGFloat distance = (contentHeight  - height) - yOffset;

if(distance < 0)
{
    return ;
}

CGPoint offset = scrollView.contentOffset;

offset.y += distance;

[scrollView setContentOffset:offset animated:YES];

答案 3 :(得分:0)

万一,如果你想要Swift版本:

scrollView.setContentOffset(CGPointMake(0, max(scrollView.contentSize.height - scrollView.bounds.size.height, 0) ), animated: true)

希望这有帮助!

答案 4 :(得分:0)

这更可靠

CGSize contentSize = scrollview.contentSize;
[scrollview scrollRectToVisible: CGRectMake(0.0,
                                            contentSize.height - 1.0,
                                            contentSize.width, 
                                            1.0)
                       animated: YES];
相关问题