【问题标题】:scrolling the scrollview at the bottom programmatically - iphone以编程方式滚动底部的滚动视图 - iphone
【发布时间】:2011-01-13 19:28:26
【问题描述】:

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

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

谁能给我一些示例代码?

谢谢,
小屁屁

【问题讨论】:

    标签: iphone uiscrollview


    【解决方案1】:

    我稍微修改了@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 的底部,但仅在需要时才滚动(否则会出现奇怪的上/下跳跃效果)

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

    【讨论】:

      【解决方案2】:

      改用这样的东西:

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

      如果您不想让它动画化,只需将 YES 更改为 NO

      【讨论】:

        【解决方案3】:
        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];
        

        【讨论】:

          【解决方案4】:

          如果你想要 Swift 版本:

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

          希望这会有所帮助!

          【讨论】:

            【解决方案5】:

            这个比较靠谱

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

            【讨论】:

              猜你喜欢
              • 2018-11-22
              • 2011-09-22
              • 2010-10-31
              • 2019-05-23
              • 2016-01-30
              • 1970-01-01
              • 2018-12-13
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多