添加视图控制器的视图作为子视图会改变它的框架吗?

发布于 2024-10-16 05:23:27 字数 733 浏览 4 评论 0原文

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

太阳公公是暖光 2024-10-23 05:23:27

SomeViewController 是什么类型的视图控制器?我认为可能发生的一件事是 SomeViewController 可能包含一些额外的内容,例如 UINavigationController 的顶部栏,并且有时会自动增加大小。但是您所做的是将一个视图控制器的视图添加到另一个视图控制器,从而违反了 HIG。 UIViewController 被设计为屏幕的完整视图或属于专用视图控制器,例如 UITabBarControllerUINavigationControllerUISplitViewController。因此,someViewController 将无法按预期运行,也不会接收诸如 viewDidLoad 之类的调用。因此,如果您确定没有设置自动调整大小属性,我可以建议使用 UIView 而不是 UIViewController

What kind of view controller is SomeViewController ? The one thing I think that may be happening is SomeViewController may contain some extra content such as the top bar of an UINavigationController 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. An UIViewController is designed to be the full view of the screen or belonging to a specialized view controller such as an UITabBarController, UINavigationController, or UISplitViewController. As a result someViewController will not function as expected and not receive calls such as viewDidLoad. So if you are sure that no autoresizing properties are set all I can recommend is using an UIView instead of an UIViewController.

深海夜未眠 2024-10-23 05:23:27

这可能是由视图的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文