Different subview layouts in viewDidLoad and viewWillAppear [duplicate]
Asked Answered
D

1

5

Possible Duplicate:
UIViewController returns invalid frame?

While debugging i've noticed that in viewDidLoad call my view frame is origin=(x=0, y=20) size=(width=320, height=460) which is not accurate. In viewWillAppear call it is set up correctly: origin=(x=0, y=0) size=(width=320, height=416) taking into consideration navigation bar height. My subviews layout depends on root view, but i don't want to set up them each time view will appear.

How should we deal with these two calls?

Deal answered 19/12, 2012 at 17:19 Comment(1)
Deal with them exactly like you have been. Do view setup in -viewWillAppear, because that's the one that accounts for the nav bar height, then do initialization in -viewDidLoad.Robbi
M
7

The proper place to deal with view layout is in the UIViewController viewWillLayoutSubviews method. This is called whenever the view controller's view is sized such as when first shown and when rotated.

As you have seen, the view controller's view has not been fully sized yet when viewDidLoad is called.

If you need to support iOS 4.3 then you can't use viewWillLayoutSubviews since it was added in 5.0. In this case, do the layout in viewWillAppear:.

Mouthwatering answered 19/12, 2012 at 17:23 Comment(1)
It should be noted that -viewWillAppear MAY be called before the views frame is set and the window has finished orienting itself. If you need that in iOS 4 you need to wait for -viewDidAppear to be called.Bezique

© 2022 - 2024 — McMap. All rights reserved.