Search code examples
objective-cuiviewuiviewcontrolleraddsubview

UIViewController present another UIViewController


I Have a UIViewController 1 on it UIButton (added as subview), after I pressed Button (see pic.1 below) on it adding another UIViewController 2 with animation from the bottom to top after some action:

[[[UIApplication sharedApplication] keyWindow] addSubview:self.view];

And it overlaps UIButton, how can I add this subview that it does not cover UIButton(see pic.2 below)

pic.1:

enter image description here

pic. 2:

enter image description here


Solution

  • You haven't shown the animation code but I assume you add the subview off-screen, then change it's frame (animated) to slide it into view.

    Add the new subview using insertSubview:belowSubview: instead, passing your button as the second argument. This way the button will overlap the new view, instead of the other way round. addSubview: always puts the new view on top of any others.

    EDIT

    From your comments it seems that you're adding the second view controller to the screen using presentModalViewController: so the above method won't work. As far as I know there is no way to keep an element from the original view controller on top of the new view controller's view if you are presenting it this way.

    You may have to create a new UIWindow and set it's windowLevel to UIWindowLevelAlert to hold your button. This will keep it on top of any of the views underneath. Add this window as a subview to the main window.