Offset on UIWindow addSubview

前端 未结 4 2000
无人及你
无人及你 2020-12-02 06:42

I\'ve got a UITabBar based application that works just fine. Under certain circumstances I am showing a different UIViewController instead though. Now what bugs me is that t

相关标签:
4条回答
  • 2020-12-02 07:00

    Adding the root view to your UIWindow can be complicated since the window always underlaps the status bar. The frame of your root view must therefore be reset to [[UIScreen mainScreen] applicationFrame] to prevent it from underlapping the status bar as well. We normally don't have to worry about this because UIViewController modifies the frame for us... except when it doesn't. Here's the deal:

    • If you create your view controller and its view in the same NIB, and you nest the view underneath the view controller, it will adjust the view's frame automatically.
    • If you create your view controller and its view in the same NIB, but you connect the view to the view controller through the controller's view outlet rather than nesting it, the controller will not adjust the view's frame automatically.
    • If you create your view controller in one NIB, and you connect it to a view defined in a detached NIB by setting the view controller's "NIB Name" property in IB, it will adjust the view's frame automatically, but only if you also have "Resize view from NIB" checked.
    • If you create your view controller by calling -initWithNibName:bundle:, it will not adjust the view's frame automatically.
    • UITabBarController expects its view to be added as the root view of the window, and therefore always adjusts its own view's frame to match the application frame automatically. (As a result you'll notice a weird 20 pixel gap if you ever add a UITabBarController's view as a subview of anything other than the window.)

    I guess Apple figured that -initWithNibName:bundle: wouldn't typically be used to create the window's root view, so it doesn't adjust the frame in your case. Resizing it manually as you have done is fine, and is in fact recommended in the View Controller Programming Guide for iPhone OS, but you should really use [[UIScreen mainScreen] applicationFrame] since the status bar isn't always 20 pixels tall (e.g. it's taller when you're on a phone call.)

    0 讨论(0)
  • 2020-12-02 07:08

    I am assuming that your UITabBarController is an IB outlet, so that when applicationDidFinishLaunching: is called, it is already initialized. Try the following: just after instantiating your view controller, do:

    [vc setWantsFullScreenLayout:YES];
    
    0 讨论(0)
  • 2020-12-02 07:20

    This is much simpler and also works (iOS 4.0 and later)

    MyRootViewController *vc = [[MyRootViewController alloc] init];    
    [window setRootViewController:vc];
    [vc release];
    

    -setRootViewController automatically adds the view of the controller to the window so you don't need to worry about it. The property is (nonatomic, retain) so releasing it after assigning it to the window effectively hands over ownership of the object to the UIWindow and will be released (and consequently deallocated) when the window is deallocated. You could of course create an instance variable and keep a reference to it and release in -dealloc if you want to do stuff with it in the other app delegate methods. I prefer the above method as it takes care of cleanup automatically.

    In case you don't know, you can also release view controllers immediately after adding their views to any other view as a subview or presenting a modal view controllers view. Whenever a view is removed or popped off the view stack their corresponding controllers will be released as well.

    You don't need to use initWithNibName, just ...alloc] init]; will do.

    0 讨论(0)
  • 2020-12-02 07:21

    Just to second what was said above: Here's the code I had to use:

    #define MAIN_SCREEN_OFFSET_PIXELS     20
    
    - (void) pushDownViewOnMainScreen {
      CGRect r = [[UIScreen mainScreen] applicationFrame];
      r.origin.y -= MAIN_SCREEN_OFFSET_PIXELS;
    
      self.view.frame = r;
    }
    
    
    - (void) viewDidLoad {
      [self pushDownViewOnMainScreen];
    
      // ...
    }
    
    0 讨论(0)
提交回复
热议问题