蘋(píng)果的文檔是這樣說(shuō)的:
This method creates a parent-child relationship between the current view controller and the object in thechildController parameter. This relationship is necessary when embedding the child view controller’s view into the current view controller’s content. If the new child view controller is already the child of a container view controller, it is removed from that container before being added.
This method is only intended to be called by an implementation of a custom container view controller. If you override this method, you must callsuper in your implementation.
大意就是要求,如果 B VC 的 view 作為 A VC 的 view 的 subView.那么也應(yīng)該調(diào)用 addChildViewController 把B VC 設(shè)置為 A VC 的 childViewController.
這樣做有幾個(gè)好處:
- 在 A VC 參與 ViewController 跳轉(zhuǎn)的時(shí)候,可以把生命周期回調(diào)(viewDidLoad viewWillAppear 等)傳給 B VC.
- B VC 可以拿到 A VC 的 navigationItem 等對(duì)象,方便控制.
- B VC 的 view 的 frame 受到 A VC 的控制.
這樣一看好處不少,但是有個(gè)坑.
平時(shí)Status Bar 的高度是20px, 但是打電話或者錄音的時(shí)候再進(jìn)入 app,Status Bar 高度會(huì)變成40px. 這個(gè)時(shí)候,系統(tǒng)會(huì)自動(dòng)把 UIWindow 的 rootViewController.view 下移20px.
但是不知道為什么,調(diào)用addChildViewController以后,B VC 的view 也會(huì)下移20px, 這樣以來(lái), StatusBar 高度增加了20px, 但是 B VC 累計(jì)向下移動(dòng)了40px就導(dǎo)致 B VC 與 StatusBar 之間有一個(gè)20px 的空隙,并且通話結(jié)束后不會(huì)恢復(fù).
所以,addChildViewController 以后,還是要手動(dòng)設(shè)置一下 B VC 的 view 的 frame 來(lái)避免這個(gè) BUG.
http://stackoverflow.com/questions/17192005/what-does-addchildviewcontroller-actually-do