![](/img/trans.png)
[英]Difference between directly setting a controller's view and adding view as a subview
[英]Adding a view controller's view as a subview changes it's frame?
SomeViewController *someVC = [[SomeViewController alloc] initWithNibName:@"SomeViewController" bundle:nil];
self.someViewController = someVC;
[someVC release];
NSLog(@"height before added as subview: %f", self.someViewController.view.frame.size.height);
[self.containerView addSubview:self.someViewController.view];
NSLog(@"height AFTER added as subview: %f", self.someViewController.view.frame.size.height);
// height before added as subview: 480.000000
// height AFTER added as subview: 540.000000
有什么可能的解释呢? someViewController的视图没有设置任何自动调整大小属性,containerView不会自动调整其子视图的大小。 只是将其添加为子视图,是什么导致框架发生变化?
另一个神秘之处在于,它仅在someViewController的视图高度> = 480时发生。
这可能是由于视图的autoresizingMask引起的,其目的是调整大小以适应其超级视图中的更改。
SomeViewController
是SomeViewController
视图控制器? 我认为可能发生的一件事是SomeViewController
可能包含一些额外的内容,例如UINavigationController
的顶部栏,并且有时会自动增加大小。 但是,您正在执行的操作是通过将一个视图控制器的视图添加到另一视图控制器来违反HIG。 UIViewController
设计为屏幕的完整视图,或者属于专用的视图控制器,例如UITabBarController
, UINavigationController
或UISplitViewController
。 结果, someViewController
将无法按预期运行,并且不会收到诸如viewDidLoad
调用。 因此,如果您确定未设置自动调整大小属性,那么我所建议的只是使用UIView
而不是UIViewController
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.