Programmatically scroll a UIScrollView to the top of a child UIView (subview) in Swift

后端 未结 11 1602
遇见更好的自我
遇见更好的自我 2020-12-29 03:35

I have a few screens worth of content within my UIScrollView which only scrolls vertically.

I want to programmatically scroll to a view contained somewhere in it\'s

11条回答
  •  南方客
    南方客 (楼主)
    2020-12-29 03:35

     scrollView.scrollRectToVisible(CGRect(x: x, y: y, width: 1, height:
    1), animated: true)
    

    or

    scrollView.setContentOffset(CGPoint(x: x, y: y), animated: true)
    

    Another way is

    scrollView.contentOffset = CGPointMake(x,y);
    

    and i do it with animated like this

    [UIView animateWithDuration:2.0f delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
    scrollView.contentOffset = CGPointMake(x, y); }
    completion:NULL];
    

提交回复
热议问题