is it possible to determine whether my UIView
is visible to the user or not?
My View is added as subview
several times into a Tab Bar Controller
.
Each instance of this view has a NSTimer
that updates the view.
However I don't want to update a view which is not visible to the user.
Is this possible?
Thanks
Parent view is the view in which we are searching for descendant view and check wether added to parent view or not. if parentView. subviews. contains(descendantView) { // descendant view added to the parent view. }
The UIView class is a concrete class that you can instantiate and use to display a fixed background color. You can also subclass it to draw more sophisticated content.
For anyone else that ends up here:
To determine if a UIView is onscreen somewhere, rather than checking superview != nil
, it is better to check if window != nil
. In the former case, it is possible that the view has a superview but that the superview is not on screen:
if (view.window != nil) { // do stuff }
Of course you should also check if it is hidden
or if it has an alpha > 0
.
Regarding not wanting your NSTimer
running while the view is not visible, you should hide these views manually if possible and have the timer stop when the view is hidden. However, I'm not at all sure of what you're doing.
You can check if:
view.superview != nil
The only other thing I can think of is if your view is buried behind others and can't be seen for that reason. You may have to go through all the views that come after to see if they obscure your view.
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