Adding a UIView to the topmost layer

Discussion in 'iOS Programming' started by dantastic, Mar 9, 2011.

  1. dantastic macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #1
    I have an app with a tabbar controller, in one of the tabs I have a navigation controller. At one point stepping through the navigation controller a tableviewcontroller is loaded.

    I create a UIView and stick controls on there, uipicker, buttons, other. I want this view to be on top of everything else, including the tabs down the bottom.

    In a few windows doing this worked fine:
    Code:
    [self.view.superview.superview.superview.superview.superview.superview.superview addSubview:controlsView]
    
    I felt a bit dirty after but it works.

    I've yet another page with a very similar setup and this method doesn't work at all. In order to see the controls at all I have to go all the way down to
    Code:
    [self.view.superview addSubview:controlsView];
    
    This means that my controls are above the tableview, so I can interact with them but they are under the tab bar.

    I fear I'm implementing what would be best considered a hack though. Surely there's a *standard*, approved, not frowned upon, method of sticking a UIView on top of everything else. (ActionSheet won't do it here.)
     
  2. ppilone macrumors 6502

    Joined:
    Jan 20, 2008
    #2
    There are a few ways to do this depending on what exactly you're trying to accomplish.

    If all you need/want is to add a view on top of everything (including your tab bar controller's view), you can add it as a subview to the window. As long as your app delegate has an outlet for the window (which is standard) you can make this call from anywhere in the application:

    Code:
    [[[[UIApplication sharedApplication] delegate] window] addSubview:<view>]
    You can also get a reference to the window from any UIView that has already been added to it (i.e. your tab bar controller's view) using the window property of UIView. This might be a littler cleaner than reaching back to the delegate depending on where you're executing the code.

    That being said, it seems odd to have a view appearing on top of the tab bar controller's view that isn't a modal view controller. Usually if you want a view to take up the entire screen you would present it as the view of a modal view controller. You can create a view controller for the view and present it using presentModalViewController:animated: on UIViewController.

    Again, not exactly sure what you're accomplishing but any of those 3 suggestions should help you out.
     
  3. dantastic thread starter macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #3
    Perfect!

    Code:
    [[[[UIApplication sharedApplication] delegate] window] addSubview:<view>]
    Did the trick.

    In my case I'm in the middle of filling out a form and I'm sliding a uipicker up from the bottom. While editing all other fields the tab bar down at the bottom is covered by a keyboard or a num pad but the picker view inconveniently slid up underneath the tab bar. problem solved. cheers!
     

Share This Page