Swift - UIScrollView scroll animation not working in viewDidLoad()

user2430797

I'm developing an application using Storyboard with Xcode 12.3 and I need to animate the scrolling of a scrollView right after the view loads up on the screen and also when the user returns from another view controller to this view. It seems to me that the animation does not work if I scroll the view from within the viewDidLoad() function or from the function called when unwinding a segue. Instead, the view appears on the screen (in both cases) with the scrollview already scrolled at the respective location. Essentially, the end result is achieved but the scrolling animation is seemingly ignored or not executed.

Could anyone please help with this problem?

Here's my simple code that demonstrates the issue in both instances:

override func viewDidLoad {
    super.viewDidLoad()

    // ...

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


@IBAction func myUnwindAction(unwindSegue: UIStoryboardSegue) {
    let segue = unwindSegue.source as! AnotherViewController

    // ...

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

However, if I place the scrolling code in the IBAction of a test button and I click on this button after the view loads up, the view animates the scrolling smoothly and without any issues - Here's the code for this function:

@IBAction func testButton(_ sender: Any) {
    scrollView.setContentOffset(CGPoint(x: xOffset, y: yOffset), animated: true)
}

Many thanks.

Tên

You need to use in viewDidAppear

you should google "View Controller Life Cycle"

var isFirst = true
override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    if isFirst {
        isFirst = false
        scrollView.setContentOffset(CGPoint(x: xOffset, y: yOffset), animated: true)
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Swift-UIScrollView滚动动画在viewDidLoad()中不起作用

来自分类Dev

UIScrollView - scroll or swipe?

来自分类Dev

调用方法ViewDidload Swift

来自分类Dev

Swift Scroll to UITextField 通过 UiscrollView 只在第一次出现错误

来自分类Dev

UITableView in UIScrollView not responding to first tap after scroll

来自分类Dev

在viewDidLoad中带有参数的SWIFT函数

来自分类Dev

Swift中viewdidload内部的@IB Action函数

来自分类Dev

使用popViewController SWIFT时调用viewdidload

来自分类Dev

Swift错误从viewDidLoad中的Nib解开nil

来自分类Dev

iOS Swift:UIScrollView中的UIPanGestureRecognizer

来自分类Dev

UIScrollView无法在Swift中滚动

来自分类Dev

Swift-UIScrollView中的UISwipeGestureRecognizer

来自分类Dev

禁用UIScrollView惯性(scrollViewDidEndDecelerating)Swift

来自分类Dev

Swift中的UITableViewCell中的UIScrollView

来自分类Dev

UIScrollView doesn't always scroll driving me crazy

来自分类Dev

Swift-以编程方式创建垂直的UIScrollView

来自分类Dev

Swift-UIScrollView的约束问题(以编程方式)

来自分类Dev

在Swift中将UIScrollView与UIVisualEffectView(UIBlurEffect)结合使用

来自分类Dev

使用Swift在UIScrollView中居中图像

来自分类Dev

在Swift中以编程方式编写UIScrollView

来自分类Dev

平滑移动contentoffset UIScrollView Swift

来自分类Dev

iOS UIScrollView的默认contentOffset错误-Swift

来自分类Dev

Swift UIScrollView:键盘不会以交互方式关闭

来自分类Dev

UIScrollView中的Swift UITableView(无需滚动)

来自分类Dev

Swift:使UIScrollView产生“失焦”效果

来自分类Dev

UIScrollView无法在使用Swift的UIView中工作

来自分类Dev

Swift 3 水平多个 UIScrollView - 不同的速度

来自分类Dev

滚动时为 UIScrollView 分配函数(Swift)

来自分类Dev

处理未知大小的 UIView/UIScrollView (AutoLayout/Swift)

Related 相关文章

  1. 1

    Swift-UIScrollView滚动动画在viewDidLoad()中不起作用

  2. 2

    UIScrollView - scroll or swipe?

  3. 3

    调用方法ViewDidload Swift

  4. 4

    Swift Scroll to UITextField 通过 UiscrollView 只在第一次出现错误

  5. 5

    UITableView in UIScrollView not responding to first tap after scroll

  6. 6

    在viewDidLoad中带有参数的SWIFT函数

  7. 7

    Swift中viewdidload内部的@IB Action函数

  8. 8

    使用popViewController SWIFT时调用viewdidload

  9. 9

    Swift错误从viewDidLoad中的Nib解开nil

  10. 10

    iOS Swift:UIScrollView中的UIPanGestureRecognizer

  11. 11

    UIScrollView无法在Swift中滚动

  12. 12

    Swift-UIScrollView中的UISwipeGestureRecognizer

  13. 13

    禁用UIScrollView惯性(scrollViewDidEndDecelerating)Swift

  14. 14

    Swift中的UITableViewCell中的UIScrollView

  15. 15

    UIScrollView doesn't always scroll driving me crazy

  16. 16

    Swift-以编程方式创建垂直的UIScrollView

  17. 17

    Swift-UIScrollView的约束问题(以编程方式)

  18. 18

    在Swift中将UIScrollView与UIVisualEffectView(UIBlurEffect)结合使用

  19. 19

    使用Swift在UIScrollView中居中图像

  20. 20

    在Swift中以编程方式编写UIScrollView

  21. 21

    平滑移动contentoffset UIScrollView Swift

  22. 22

    iOS UIScrollView的默认contentOffset错误-Swift

  23. 23

    Swift UIScrollView:键盘不会以交互方式关闭

  24. 24

    UIScrollView中的Swift UITableView(无需滚动)

  25. 25

    Swift:使UIScrollView产生“失焦”效果

  26. 26

    UIScrollView无法在使用Swift的UIView中工作

  27. 27

    Swift 3 水平多个 UIScrollView - 不同的速度

  28. 28

    滚动时为 UIScrollView 分配函数(Swift)

  29. 29

    处理未知大小的 UIView/UIScrollView (AutoLayout/Swift)

热门标签

归档