0

I want to add a view:

[aView addSubview:myNewView];

What I am trying to do is find what view is the topmost view being displayed at any given moment, so I can dynamically add a view on top of it. For example, let's say my view stack looks like this:

  1. Modal Camera View (on top of everything, currently being displayed)
  2. TabView
  3. Navigation View
  4. View
  5. TableView

I want to dynamically put something on top of whatever view is at 0.

Does anyone know how to do this or if it's even possible?

Ethan Allen
  • 13,099
  • 22
  • 89
  • 175
  • [self.view subviews] returns an NSArray. You can access the view at index 0 by [self.view subviewAtIndex:0]; – Zen Jun 05 '13 at 20:03
  • Do you mean in general, or are you specifically trying to overlay a view ontop of the view from a `UIImagePickerController`? – Graham Jun 05 '13 at 20:05

1 Answers1

3

The view hierarchy is not represented by a stack but by a tree, so there is nothing as the topmost view.

If you just want to add a view on top of all other views, add it to UIWindow as done in this answer :

UIWindow* mainWindow = [[UIApplication sharedApplication] keyWindow];
[mainWindow addSubview:myNewView];

If by view you really meant view controller, then I don't think there way to "walk" the current stack because it would depend on the classes of the stacked view controllers, and also for the same reason as the view hierachy: a view controller could have many childs - a UITabBarController for example.

Community
  • 1
  • 1
Guillaume Algis
  • 10,000
  • 5
  • 39
  • 68