Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

viewWillAppear for subviews

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. :)

like image 319
nan Avatar asked Jun 01 '10 13:06

nan


People also ask

What is difference between viewWillAppear and viewDidAppear?

As the name suggests the viewWillAppear is called before the view is about to appear and viewDidAppear is called when view did appear.

Is viewDidLoad called before viewWillAppear?

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.


2 Answers

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.

like image 161
Ole Begemann Avatar answered Sep 20 '22 20:09

Ole Begemann


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")     } } 
like image 37
Lunarchaos42 Avatar answered Sep 19 '22 20:09

Lunarchaos42