确定loadView中正确的大小
如何一致地确定在 loadView 中布局视图的正确尺寸?
我有不同的情况,其中 UIViewController 被推送到 UINavigationController,或 UITabBarController 存在,或两者兼而有之。这些元素会更改可用的屏幕区域,并在我对值进行硬编码时使其不一致。
在调用 loadView 时,我想知道如何调整我添加子视图的容器视图的大小。
我浏览了 UIViewController 指南,苹果使用 UIScreen mainScreen applicationFrame,但在他们的示例中这样做是因为他们正在制作全屏应用程序。
我在这里看到了一些答案,但没有一个解决如何以一致的方式做到这一点。
感谢您提供的任何帮助。
How to consistently determine the correct size to layout your view in loadView?
I have different cases where a UIViewController is pushed to a UINavigationController, or a UITabBarController is present, or both. These elements changes the available screen area and makes it inconsistent if I hard code values.
At the moment where loadView is called and I want to know how to size the container view I add my subviews to.
I looked through the UIViewController guide and Apple uses UIScreen mainScreen applicationFrame, but in their example this is done because they are making a full screen app.
I have seen some answers in here, but none that addresses how to do this in a consistent manner.
Thanks for any help given.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不需要知道确切的尺寸。风险投资公司不应该自行调整规模,这是其母公司的责任。只需使用
[[UIView alloc] init]
创建一个UIView
,Apple UIKit 工程师 Andy Matuschak 在 Twitter 上确认:https://twitter.com/andy_matuschak/status/365227892151558144You don't need to know the exact size. A VC shouldn't have to size itself, that's the responsibility of its parent. Just create a
UIView
with[[UIView alloc] init]
as confirmed by Apple UIKit Engineer Andy Matuschak on Twitter: https://twitter.com/andy_matuschak/status/365227892151558144来自苹果的 视图控制器编程指南,用于以编程方式创建视图:
然后它有一个与我上面写的示例类似的示例。
From Apple's View Controller Programming Guide for creating view programmatically:
Then it has an example which is similar to the one I've written above.
“正确”的答案可能是创建一个自定义 UIView 子类,并在覆盖layoutSubviews 时完成所有子视图视图定位。然后您的子类将被分配给 loadView 中的 self.view 。
但是,使用 viewDidLoad 将相同大小的子视图添加到 self.view 也可以。 (尽管您应该为此视图更改大小做好准备。--正确设置 autoresizingMasks 可能是实现此目的的好方法。)
The "right" answer is probably to create a custom UIView subclass and do all of the subview view positioning in your override of layoutSubviews. Your subclass would then get assigned to self.view in loadView.
However, it also works to use viewDidLoad to add a subview of identical size to self.view. (Though you should be prepared for this view to change size. -- Correctly setting autoresizingMasks can be a good way to do this.)
我制作了一个应用程序,它加载几个 UITextViews、UIButtons 和一个 UISegment 控件。我在 viewWillAppear 中所做的,称为我所做的调整大小方法。
它以编程方式更改 UIViewController 中所有子视图的大小。这是因为 IB 中的自动调整大小效果仅此而已。
在我的 calcViewSize 函数中,我得到这样的尺寸;
然后我遍历 UIViewController 中要调整大小的每个子视图,并更改其框架。我在 IBOutlet 中有这些视图的属性,因此我已经知道这些视图是什么。
使用 Interface Builder 获取尺寸。
最后一点注意:要更改 IB 中的尺寸,请选择视图,选择属性检查器,禁用状态栏,现在在尺寸检查器中,您可以调整视图大小,重新排列子视图,并记下要放入代码中的数字。
I have made an app, it loads several UITextViews, UIButtons, and a UISegment control. What I did in viewWillAppear, I call a resize method I made.
It changes the sizes of all subviews in the UIViewController programmatically. This is because autoresizing in IB is only so good.
In my calcViewSize function, I get the dimensions like this;
Then I walk through each subview in the UIViewController that I want to resize, and change its frame. I have properties for these views in IBOutlets, so I already know what the views are.
Use Interface Builder to get the dimensions.
One last Note: To change dimensions in IB, select the View, select attributes inspector, disable the Status Bar, now in Size Inspector, you can resize the view, re-arrange your subviews, and write down the numbers to place in your code.