如果在一個UIViewController中有如下代碼,你可能就在誤用UIViewController
<pre><code>
viewController.view.bounds = CGRectMake(50, 50, 100, 200);
[viewController.view addSubview:someOtherViewController.view];
</code></pre>
官方添加subView的方式
Listing 14-1 Adding another view controller’s view to the container’s view hierarchy
<pre><code>
-(void) displayContentController: (UIViewController*) content;
{
[self addChildViewController:content]; // 1
content.view.frame = [self frameForContentController]; // 2
[self.view addSubview:self.currentClientView];
[content didMoveToParentViewController:self]; // 3
}
</code></pre>
Listing 14-2 Removing another view controller’s view to the container’s view hierarchy
<pre><code>
-(void) hideContentController: (UIViewController*) content
{
[content willMoveToParentViewController:nil]; // 1
[content.view removeFromSuperview]; // 2
[content removeFromParentViewController]; // 3
}
</code></pre>
參考連接: