添加视图控制器的视图作为子视图会改变它的框架吗?
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 时,才会发生这种情况。
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
What possible explanation is there for this? someViewController's view does not have any autoresizing properties set, and containerView does not autoresize it's subviews. What could cause the frame to change by just adding it as a subview?
A further mystery to this is that it only happens when someViewController's view height is >= 480.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
SomeViewController
是什么类型的视图控制器?我认为可能发生的一件事是SomeViewController
可能包含一些额外的内容,例如UINavigationController
的顶部栏,并且有时会自动增加大小。但是您所做的是将一个视图控制器的视图添加到另一个视图控制器,从而违反了 HIG。UIViewController
被设计为屏幕的完整视图或属于专用视图控制器,例如UITabBarController
、UINavigationController
或UISplitViewController
。因此,someViewController
将无法按预期运行,也不会接收诸如viewDidLoad
之类的调用。因此,如果您确定没有设置自动调整大小属性,我可以建议使用UIView
而不是UIViewController
。What kind of view controller is
SomeViewController
? The one thing I think that may be happening isSomeViewController
may contain some extra content such as the top bar of anUINavigationController
and that is automatically increasing the size sometimes. But what you are doing is violating the HIG by adding the view of one view controller to another view controller. AnUIViewController
is designed to be the full view of the screen or belonging to a specialized view controller such as anUITabBarController
,UINavigationController
, orUISplitViewController
. As a resultsomeViewController
will not function as expected and not receive calls such asviewDidLoad
. So if you are sure that no autoresizing properties are set all I can recommend is using anUIView
instead of anUIViewController
.这可能是由视图的 autoresizingMask 引起的,它的目的是调整大小以适应其超级视图的变化。
This can be caused by the autoresizingMask of the view, it's purpose is to adjust the size to changes in it's super view.