0

A view in a view controller created using a nib has its frame at {0, 64, 320, 416}, origin starting right below the nav bar. In -loadView without a nib however, the view with the same frame is 44 pt off despite having the same frame (origin=(x=0, y=64) size=(width=320, height=416)), origin starting below the status bar, under the nav bar. Why is this and how can I match the nib behavior?

-(void)loadView
{
    CGRect f = EPFrame;
    if(self.navigationController)
    {
        f.size.height -= self.navigationController.navigationBar.bounds.size.height;
        f.origin.y += self.navigationController.navigationBar.bounds.size.height;

    }
    UIView *base = [[UIView alloc]initWithFrame:f];
    base.backgroundColor = [UIColor clearColor];
    self.view = base;

}
Morrowless
  • 6,856
  • 11
  • 51
  • 81
  • you dont need to adjust view for navigation. It x and y starts below navigation bar. So don't increase y. It will work fine. – Durgaprasad Jul 04 '13 at 05:36

2 Answers2

2

There is no need, in this case, to implement the loadView method. By default the view controller will get an empty view. This view will be autosized to fit its container.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • The view I let loadView create has its frame as (0 20; 320 460), whereas I expect it to be (0, 0, 320, 416). – Morrowless Jul 04 '13 at 06:01
  • 1
    When are you checking the frame? It won't get properly resized until after `viewDidLoad` is called. – rmaddy Jul 04 '13 at 06:02
  • I see. Well, in -viewDidAppear, its (0 0; 320 460). – Morrowless Jul 04 '13 at 06:04
  • Is the view controller's `wantsFullScreenLayout` set to YES? Is the containing navigation controller's navbar set to be translucent? – rmaddy Jul 04 '13 at 06:07
  • wantsFullScreenLayout has not been changed, so NO by default. Not translucent either. – Morrowless Jul 04 '13 at 06:21
  • Sorry, the nav bar was indeed set to translucent = YES. Why does this affect frame size? – Morrowless Jul 04 '13 at 06:42
  • @Plenilune When the navbar is translucent, the view controller's view is sized to be under the navbar. The whole point of a translucent navbar is so you can see the view under it. That is the view controller's view. – rmaddy Jul 04 '13 at 14:43
0

As I put the comment. You don't need to change view.frame.

self.view starts below navigation controller if it is enabled. If you don't have navigation self.view starts below status bar. If you hide status bar then self.view starts right from top of screen.

IOS takes care this internally. You don't need to do any code for this.

Durgaprasad
  • 1,910
  • 2
  • 25
  • 44
  • So I let -loadView create its view (didn't override it), but self.view actually starts below status bar, above the nav bar for me. frame is (0 20; 320 460). – Morrowless Jul 04 '13 at 05:58
  • @rmaddy told about viewDidApper. View gets correct frame after view appears. In viewDidLoad you cannot be sure of correct frame. – Durgaprasad Jul 04 '13 at 06:06
  • nav = [[MyNavViewController alloc]initWithRootViewController:vc]; self.window.rootViewController = nav; [self.window makeKeyAndVisible]; – Morrowless Jul 04 '13 at 06:18
  • It's done programmatically, I know from experience there are view related problems that arise from not using Main.xib to create your root view. (this isn't my code by the way) Wonder if it can be worked around. – Morrowless Jul 04 '13 at 06:20
  • The window is also created programmatically: self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; – Morrowless Jul 04 '13 at 06:27
  • that is also right. Log frame of view in viedDidAppear and viewDidLoad. See what are the results. Dont do anything in loadView – Durgaprasad Jul 04 '13 at 06:31
  • frame = (0 20; 320 460) in viewDidLoad, frame = (0 0; 320 460) in viewDidAppear. This shows that the view has been adjusted, but somehow not correctly.. – Morrowless Jul 04 '13 at 06:39
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/32862/discussion-between-durgaprasad-and-plenilune) – Durgaprasad Jul 04 '13 at 06:43