布局&UIViewController 中子视图的大小 [英] Laying out & sizing of subviews in a UIViewController

查看:16
本文介绍了布局&UIViewController 中子视图的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 UITabController 的应用程序,每个选项卡都是一个 UINavigationController.我的一个 UINavigationControllers 的根是一个 UIViewController.

I have an app with with a UITabController and each tab is a UINavigationController. The root of one of my UINavigationControllers is a UIViewController.

在该视图控制器的视图中,我想布局一些子视图,但我不知道在哪里 &如何以独立于分辨率的方式布置它们(即不是硬编码值,例如 320px、480px、44px 等).

Inside that view controller's view, I want to layout some subviews, but I'm confused as to where & how to lay them out in a way that will be resolution independent (i.e. not hardcode values such as 320px, 480px, 44px, etc.).

当视图完全加载并呈现在垂直 iPhone 上时,它的高度将为 367px = 480 - 20(状态栏)- 44(导航栏)- 49(标签栏).

When the view is fully loaded and presented on a vertical iPhone, it's height will be 367px = 480 - 20 (status bar) - 44 (nav bar) - 49 (tab bar).

在视图控制器中,我目前在 viewDidLoad 方法中创建了我的所有子视图.但是,在此方法中,视图的当前高度似乎是 460 像素(self.view.bounds.size.height).所以在设置我的子视图时,我无法正确计算任何东西的大小.

Inside the view controller, I currently create all my subviews within the viewDidLoad method. However, it appears that within this method, the view's current height is 460px (self.view.bounds.size.height). So when setting up my subviews, I cannot properly calculate the sizes of anything.

viewWillAppear: 方法中,视图确实知道它的大小合适,但这意味着设置 &每次视图出现时计算子视图的框架(例如标签更改或从导航堆栈上的子视图控制器弹出.

Within the viewWillAppear: method, the view does know it's proper size, but that would mean setting & calculating the subview's frames every time the view will appear (e.g. tab changes or popping from child view controllers on the navigation stack.

这是在 viewWillAppear: 中正确布局的唯一方法吗?

Is the only way to do this properly to layout in viewWillAppear:?

我曾尝试使用自动调整大小属性(父级的 autoresizesSubviews & autoresizingMask),但它们似乎根本不起作用!?这些是否仅在视图全部设置然后调整大小(手动/方向更改?)后才生效.

I have tried using the autoresizing properties (parent's autoresizesSubviews & autoresizingMask) but they don't seem to work at all!? Do these only take effect once the view is all setup and then it is resized (manually / orientation change?).

如果有人能让我知道为什么自动调整大小不起作用,以及如何通过不硬编码任何尺寸来最好地布置事物,我将不胜感激.

I'd be grateful if someone could let me know why the autoresizing isn't working, and how best to lay things out by not hardcoding any sizes.

推荐答案

autoresizesSubviews 应该在你的父视图上设置,而 autoresizingMask 应该在子视图上设置 -这是我犯的错误,所以你也可以.

autoresizesSubviews should be set on your parent view, while autoresizingMask should be set on the child views - this is the mistake I made so you could, too.

loadView 中,您应该调整子视图的大小以适应当前父视图的大小,然后当父视图从 460 像素调整到 367 像素时,您的子视图将被调整大小同样,根据您上面的蒙版设置.

In loadView you should size your subviews to fit whatever size of parent view is at the moment, and then later on when parent view is resized from 460 to 367 pixels your sub-views will be resized as well, according to your mask settings above.

如果失败,则在 viewWillAppear 中设置视图大小没有任何问题 - 每次都这样做对性能的影响可以忽略不计.

If that fails, there is nothing wrong in setting the view size within viewWillAppear - the performance impact of doing it every time is negligible.

如果没有其他效果,总是有 layoutSubviews: - 如果需要,您可以在那里进行手动布局,当系统认为布局可能需要更改时会调用它.还有 setNeedsLayout: 我有时会从 viewWillRotate:/viewDidRotate: 等调用.但实际上这不应该是需要的,应该是 autoresize够好了.

If nothing else works, there is always layoutSubviews: - there you could do manual layout if you have to, it's invoked when system believes layout may have to change. there is also setNeedsLayout: that I sometimes invoke from viewWillRotate:/viewDidRotate: etc. But really this shouldn't be needed and autoresize should be good enough.

是的,要在 layoutSubviews 中实现自定义布局逻辑,正如我上面提到的,需要对 UIView 进行子类化.

Yes, to implement custom layout logic in layoutSubviews as I mention above one would need to subclass UIView.

这篇关于布局&UIViewController 中子视图的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆