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.
As a general rule, the views frame/bounds should never be relied on in
viewDidLoad.The
viewDidLoadmethod 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
viewWillAppearorviewDidAppearas 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 yourviewDidLoadandviewWillAppearmethods, you will see that only the latter method returns the actual view size in relation to any other elements wrapped around your view (such asUINavigationBarandUITabBar).