4
votes

I do everything programmatically when developing for the ios, so I manually initialize the view controllers view in loadView for all of my view controllers (this is what Apple says to do). What frustrates me about doing it this way is the viewcontrollers frame initially is {{0, 20}, {320, 460}} not correctly accounting for any navbars or tabbars I have in my app. The view finally sets its proper frame in viewDidAppear, but by that time it's too late to do anything. Is there anyway to make it recognize the view layout earlier without having calculate it manually?

It's frustrating because I always have to manually check to see if the phone is in landscape or portrait mode to account for rotation, and if I have to set up my subviews in a specific way that I can't do with autoresizing, then I also have to do manual calculations.

Is there any pattern that apple recommends for doing this, or is there anyway to get it to recognize the view layout earlier without having to calculate it manually?

2
Just curious, why do you do everything programmatically?5StringRyan
you should use [[UIScreen mainScreen] bounds] to get the current size of the viewwattson12
I just like to, and a lot of the views im making for my app are dynamic at run time so I need to.Ser Pounce

2 Answers

4
votes

If you are using iOS 5, you should get passed the correct frame in the viewDidLayoutSubviews and viewWillLayoutSubviews methods of your UIViewController subclass. This is where you should calculate your layout, and they are called when the device rotates.

Otherwise, viewWillAppear exists from iOS 2.0, and my test app shows that it is passed the proper frame. It doesn't solve the rotation mid-run, but presumably you can just hook into that elsewhere. You aren't meant to set up your views that late in the game, but other than playing around with layoutIfNeeded from viewDidLoad (that I didn't manage to get working in the past) I don't know what you are supposed to do pre iOS5.

0
votes

You can make sure your view has the right autoresizing mask, and it should all adjust once the new size comes in.

[view setAutoresizingMask:UIViewAutoresizingFlexibleHeight];

That's what I usually do. Otherwise, I adjust the layout in a later callback - it's adjusted for the nav bar by viewWillAppear, not sure if viewDidLoad is too early.