Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode's auto layout is only effective in viewDidAppear and this is very problematic

After upgrading my project to iOS 6, I realized that auto layout is only effective in viewDidAppear and most of my code expects the view's frame to be available in viewDidLoad. This limitation renders the really nice auto layout feature almost useless for me. Is there any suggestions to help me use auto layout?

For example, sometimes the developer needs to adjust information about a subview based on where auto layout chooses to place that particular subview. The subview's final location cannot be ascertained by the developer until AFTER the user has already seen it. The user should not see these information adjustments but be presented the final results all at once.

More specifically: What if I want to change an image in a view based on where auto-layout places that view? I cannot query that location and then change the image without the user seeing that happen.

like image 399
Paul Goh Avatar asked Nov 01 '12 14:11

Paul Goh


1 Answers

As a general rule, the views frame/bounds should never be relied on in viewDidLoad.

The viewDidLoad method only gets called once the view has been created either programmatically or via a .nib/.xib file. At this point, the view has not been setup, only loaded into memory.

You should always do your view layout in either viewWillAppear or viewDidAppear as these methods are called once the view has been prepared for presentation.

As a test, if you simply NSLog(@"frame: %@", NSStringFromCGRect(self.view.frame)); in both your viewDidLoad and viewWillAppear methods, you will see that only the latter method returns the actual view size in relation to any other elements wrapped around your view (such as UINavigationBar and UITabBar).

like image 62
Zack Brown Avatar answered Sep 28 '22 04:09

Zack Brown