Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell if a UIView is in front (visible)?

Tags:

iphone

uiview

I need to detect which view is in front (currently visible). How can I do this?

Here is a what I would like to do:

if ( ! <<methodToTellIfViewAIsInFront>>) {
  [viewA prepareToDisplay];
  [window bringSubviewToFront: viewA];
}
like image 467
Andrew Raphael Avatar asked Apr 12 '09 00:04

Andrew Raphael


People also ask

How do I know if a ViewController is visible?

The view's window property is non-nil if a view is currently visible, so check the main view in the view controller: Invoking the view method causes the view to load (if it is not loaded) which is unnecessary and may be undesirable. It would be better to check first to see if it is already loaded.

How do you check if a view contains a Subview?

If you need a quick way to get hold of a view inside a complicated view hierarchy, you're looking for viewWithTag() – give it the tag to find and a view to search from, and this method will search all subviews, and all sub-subviews, and so on, until it finds a view with the matching tag number.

What is a UIView?

UIView can be defined as an object by using which we can create and manage the rectangular area on the screen. We can have any number of views inside a view to create a hierarchical structure of the UIViews. The UIView is managed by using the methods and properties defined in the UIView class that inherits UIKit.


1 Answers

UIView's don't necessarily have a concept of being in front. UIWindows can be key or not, but it's not quite the same thing.

You can bring a view to the front, but that doesn't mean it is or is not visible. Remember, views can be any size.

A UIView buried deep in the hierarchy could be partially visible, it could be obscured, or it could be behind some translucent view. Likewise a view at the front may not be visible at all if its opacity value or hidden flags are modified.

I think what you want to do is check the subviews NSArray of your superview or UIWindow and check that. I can't remember which is the front, but it is either the first or last object.

Subviews are drawn with the painter's method. The views are drawn in order from farthest to nearest and the last object drawn is "the front."

like image 77
amattn Avatar answered Sep 30 '22 19:09

amattn