I have UIScrollView with multiple UIVIew subviews. I would like to update the data that is displayed by each UIView when they appear in the visible portion of the UIScrollView.
What is the callback that gets triggered? I tried viewWillAppear, but it does not seem to get called.
Thanks. :)
As the name suggests the viewWillAppear is called before the view is about to appear and viewDidAppear is called when view did appear.
viewWillAppear(_:)Always called after viewDidLoad (for obvious reasons, if you think about it), and just before the view appears on the screen to the user, viewWillAppear is called.
You have to do the calculation yourself. Implement scrollViewDidScroll:
in your scroll view delegate and calculate manually which views are visible (e.g. by checking if CGRectIntersectsRect(scrollView.bounds, subview.frame)
returns true.
Swift 3 solution
func scrollViewDidScroll(_ scrollView: UIScrollView) { let viewFrame = greenView.frame let container = CGRect(x: scrollView.contentOffset.x, y: scrollView.contentOffset.y, width: scrollView.frame.size.width, height: scrollView.frame.size.height) // We may have received messages while this tableview is offscreen if (viewFrame.intersects(container)) { // Do work here print("view is visible") } else{ print("nope view is not on the screen") } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With