Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference "top-most" view?

In iOS, programmatically, how can one find what top most UIView is?

In other words .. what view is displayed right now?

Say, i have a nib with 3 views stacked on top of one another. Inside a program, i can remove the top view if I know what it is. How can i find out what view is on top of the nib?

like image 859
James Raitsev Avatar asked Nov 04 '11 21:11

James Raitsev


2 Answers

You can have a lot of top most views because a view doesn't have to take all the screen.

If you want the top most subview of a view, you can call

[yourView subviews];

and take tje last one (they are in displayed order, most front last)

[[yourView subviews] objectAtIndex:[[yourView subviews] count]];

edit: this is better (from comments)

[[yourView subviews] lastObject];

If you know your displayed viewController just replace yourView by yourController.view

like image 182
MatLecu Avatar answered Oct 13 '22 23:10

MatLecu


Like this

UIView *topMost = [[self.view subviews] lastObject];
like image 31
hfossli Avatar answered Oct 13 '22 22:10

hfossli