本文介绍了如何在 2 个 UIViewController 上使用自动调整大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个 UIViewControllers:

UIViewControllers1
UIViewControllers2

UIViewControllers2.view 作为子视图添加到 UIViewControllers1.view.

UIViewControllers2.view is added as subView to UIViewControllers1.view.

问题是 UIViewControllers2.view 不想在 UIViewControllers1.view 调整大小时自动调整大小 (UIViewControllers1 setFrame:frame).

And the problem is that UIViewControllers2.view doesn't want to autoresize when UIViewControllers1.view is resizing (UIViewControllers1 setFrame:frame).

[UIViewControllers1.view setAutoresizesSubViews:YES]
[UIViewControllers1.view setAutoResizeMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)]
[UIViewControllers1.view addSubview:UIViewControllers2.view];

如何解决?

推荐答案

嗯,部分问题在于这是非法的:

Well, part of the problem is that this is illegal:

[UIViewControllers1.view addSubview:UIViewControllers2.view];

您绝不能随意将一个视图控制器的视图添加到另一个视图控制器的视图中.只有一种方法允许在内置父子结构之外为您执行此操作(UINavigationController、UITabBafController、UIPageViewController),那就是当您创建自己的精心定制的自定义父子结构 - 而你没有这样做.我的书中描述了这样做所需的架构:

You must never just wantonly add one view controller's view to another view controller's view. There is only one way in which that is allowed outside of a built-in parent-child structure that does it for you (UINavigationController, UITabBafController, UIPageViewController), and that is when you create your own elaborate custom parent-child structure - and you are not doing that. The architecture needed for doing that is described in my book here:

http://www.apeth.com/iOSBook/ch19.html#_container_view_controllers

需要明确的是,将视图添加到另一个视图没有任何问题!让视图来来去去的能力对 iOS 来说至关重要.错误的是您不能将视图控制器的视图添加到另一个视图中,除非在父子视图控制器架构下,内置或自定义.

To be clear, there is nothing whatever wrong with adding a view to another view! The ability to make views come and go is crucial to iOS. What's wrong is that you must not add a view controller's view to another view, except under the parent-child view controller architecture, built-in or custom.

这篇关于如何在 2 个 UIViewController 上使用自动调整大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 20:58