Sizing views inside a container view controller

    今天在用container view controller時,發現我添加的三個sub view controller大小不對,原本應該是左中右三屏的。研究後發現是設置frame無效。經搜索發現是本身對View Controller Lifecycle仍是不理解(罪過啊。。)。將設置sub viewcontroller的frame的代碼移到viewDidAppear or viewWillLayoutSubviews等方法中便可。概述以下:html

"1. 在viewWillAppear以前,不管橫屏仍是豎屏,view的frame和bounds都是按豎屏方式計算的;2. 在viewWillAppear以前,navigationController(而非父view,實際上這個vc的superview是 navigationController的view的一個subview)的導航條並無計算在frame和bounds中,但電池條的寬度是一直計算 了的;3. 在轉屏時,觸發的是viewWillLayoutSubview及viewDidLayoutSubview(data not shown)。
由此結論,咱們繼續往下想,若是咱們要改變 self.view的frame值,咱們應當在哪一個方法中修改呢?很容易想到的是,init和viewDidLoad中是不行的,實踐證實,在 viewWillAppear中也是不行的,要在viewDidAppear/viewWillLayoutSubviews /viewDidLayoutSubviews方法中修改才能產生效果。"

具體緣由詳細閱讀如下連接中的文章便可:ios

http://blog.sina.com.cn/s/blog_76264a170101e5lb.htmlapp

http://www.samirchen.com/view-controller-lifecycle-in-ios/
iview

http://stackoverflow.com/questions/13079172/unable-to-set-frame-correctly-before-viewdidappear
post

http://stackoverflow.com/questions/9539676/uiviewcontroller-returns-invalid-frame
ui

http://stackoverflow.com/questions/11384696/view-controller-frame-size-in-child-controllerspa

http://quke.org/post/ios-viewcontroller-lifecycle.html
code

相關文章
相關標籤/搜索