HOW DO I TELL IF A UIVIEW IS VISIBLE AND ON SCREEN?

Mikki Halpin

Total Post:23

Points:161
Posted by  Mikki Halpin
 1013  View(s)
Ratings:
Rate this:

If I have a UIView (or UIView subclass) that is visible, how can I tell if it's currently being shown on the screen (as opposed to, for example, being in a section of a scroll view that is currently off-screen)?

 

To maybe give you a better idea of what I mean, UITableView has a couple of methods for determining the set of currently visible cells. I'm looking for some code that can make a similar determination for any given UIView.

  1. Tarun Kumar

    Post:214

    Points:1642
    Re: How do I tell if a UIView is visible and on screen?

    I recently had to check whether my view was onscreen. This worked for me:

    CGRect viewFrame = self.view.frame;

    CGRect appFrame = [[UIScreen mainScreen] applicationFrame];

     

    // We may have received messages while this tableview is offscreen

    if (CGRectIntersectsRect(viewFrame, appFrame)) {

        // Do work here

    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!