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

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

问题描述

我有一个应用程序与 UITabController ,每个选项卡是一个 UINavigationController 。我的 UINavigationControllers 之一的根是 UIViewController



在视图控制器的视图中,我想布局一些子视图,但我困惑的地方&如何以一种将是分辨率独立的方式(即不是硬编码值,如320px,480px,44px等)来布局它们。



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



视图控制器,我目前在 viewDidLoad 方法中创建所有我的subviews。但是,在此方法中,视图的当前高度为460px( self.view.bounds.size.height )。因此,在设置我的子视图时,我无法正确计算任何大小。



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



是正确布局布局的唯一方法在 viewWillAppear:



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



如果有人能告诉我为什么自动调整不工作,不要硬编码任何大小。

解决方案

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



loadView 中,您应该调整子视图的大小以适应当前的父视图的大小,然后当父视图的大小从460调整为367像素时



如果失败,在内设置视图大小没有问题, viewWillAppear - 每次执行的性能影响可以忽略不计。



如果没有其他效果,总是 layoutSubviews : - 你可以做手动布局,如果你必须,它被调用时,系统认为布局可能会改变。还有 setNeedsLayout :我有时从中调用viewWillRotate: / viewDidRotate :等等,但真的这不应该需要和autoresize应该足够好。



编辑:是的,实现自定义布局逻辑 layoutSubviews 如上所述,需要将 UIView 子类化。


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

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.).

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).

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.

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.

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

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 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.

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.

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

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.

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

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

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