4

I am trying to display a viewController xib view with its view displayed under a fixed header banner (0,0,320,44) which is an imageView added on Application window.

This is because I need it to stick on the screen while I navigate multiple view controllers. Its a requirement of the project I am doing.

So far I have tried:

  • Resizing the XIB view to the appropriate frame size (0,0,320,416), both with dot notation and setter.

  • Changing the frame in viewDidLoad with self.view.frame = CGRectMake(0, 44, 320, 416);

but it is not working. Any suggestion?

Fabrizio Prosperi
  • 1,398
  • 4
  • 18
  • 32
  • 1
    why you are adding the imageview on Application window.you can add it in viewController xib. – Mudit Bajpai May 03 '12 at 10:05
  • @Mudit: I need it to stick on the screen while I navigate multiple view controllers. Its a requirement of the project I am doing. – Fabrizio Prosperi May 03 '12 at 10:10
  • Is there any reason for you to use multiple view controllers? Seems to me you only need multiple views. In any case you can leave default full screen view controller views and add subviews below header banner. In the case of only 1 view controller there is less work then in case of multiple. – Matic Oblak May 03 '12 at 10:55
  • @Matic you are right but I want to use xibs and I don't want to mess in loading xibs from bundle at runtime, this is why I am choosing this way. Otherwise, you are totally right, I could have been using only programmatically-built views and the resize would have been easier. – Fabrizio Prosperi May 03 '12 at 12:43

2 Answers2

17

Set the frame in viewWillAppear

- (void)viewWillAppear:(BOOL)animated
{
     self.view.frame = CGRectMake(0, 44, 320, 416);
    [super viewWillAppear:animated];
}
DD_
  • 7,230
  • 11
  • 38
  • 59
sundar
  • 186
  • 2
  • 5
1

Just want to add a bit more. The reason for changing view of a UIViewController does not work in viewDidLoad is because of the call [window makeKeyAndVisible], which is usually called after your viewDidLoad. This call will change the frame of the window.rootViewController to equal the window's frame. You can test this by changing the UIViewController's view.frame AFTER [window makeKeyAndVisible], and the changes will stick.

viewWillAppear and viewDidAppear are called within [window makeKeyAndVisible] but after the view's frame got resized.

The caveat is that only the top most controller which is the rootViewController has its view's frame changed automatically. This DOES NOT affect any childViewControllers attached to the rootViewController.

langtutheky
  • 183
  • 1
  • 11