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

以编程方式滚动底部的滚动视图-iphone,iphone,uiscrollview,Iphone,Uiscrollview,我在scrollview中动态添加了一些视图,并增加了scrollview的contentsize,但我想将scrollview滚动到其高度的底部 scrollRectToVisible对我没有帮助。它只是滚动到我的iphone屏幕的可见视图,但我想到达scrollview的contentsize的底部 谁能给我一些示例代码 谢谢, Naveed Butt改用类似的方式: CGPoint bottomOffset = CGPointMake(0, [yourScrollView contentS

我在scrollview中动态添加了一些视图,并增加了scrollview的contentsize,但我想将scrollview滚动到其高度的底部

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

谁能给我一些示例代码

谢谢,
Naveed Butt

改用类似的方式:

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

如果您不想将其设置为动画,只需将“是”更改为“否”。

请使用类似以下内容:

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

如果您不想让它成为动画,只需将“是”更改为“否”。

我稍微修改了@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本身的高度,它会将内容向上滚动到可见的位置。

我稍微修改了@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];
}
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];
这会将内容滚动到UIScrollView的底部,但只有在需要这样做时,否则会出现奇怪的上下跳跃效果


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

如果您想要Swift版本:

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];
scrollView.setContentOffset(CGPointMake(0, max(scrollView.contentSize.height - scrollView.bounds.size.height, 0) ), animated: true)

希望这有帮助

如果您想要Swift版本:

scrollView.setContentOffset(CGPointMake(0, max(scrollView.contentSize.height - scrollView.bounds.size.height, 0) ), animated: true)
希望这有帮助

这更可靠

CGSize contentSize = scrollview.contentSize;
[scrollview scrollRectToVisible: CGRectMake(0.0,
                                            contentSize.height - 1.0,
                                            contentSize.width, 
                                            1.0)
                       animated: YES];
这更可靠

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