确定loadView中正确的大小

发布于 2024-12-05 12:46:47 字数 377 浏览 0 评论 0原文

如何一致地确定在 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 技术交流群。

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

发布评论

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

评论(4

椒妓 2024-12-12 12:46:48

您不需要知道确切的尺寸。风险投资公司不应该自行调整规模,这是其母公司的责任。只需使用 [[UIView alloc] init] 创建一个 UIView,Apple UIKit 工程师 Andy Matuschak 在 Twitter 上确认:https://twitter.com/andy_matuschak/status/365227892151558144

You 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

落花浅忆 2024-12-12 12:46:48
- (void) loadView {
     //self.wantsFullScreenLayout = YES; //could add this for translucent status bars
     UIView *view = [[[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame] autorelease];
     self.view = view;
}

来自苹果的 视图控制器编程指南,用于以编程方式创建视图:

  1. 创建一个大小适合屏幕的根视图对象。
    根视图充当与视图控制器关联的所有其他视图的容器。你通常
    定义此视图的框架以匹配应用程序窗口的大小,应用程序窗口本身应填充
    屏幕。然而,视图控制器也会根据需要调整帧大小以适应存在
    各种视图,例如系统状态栏、导航栏或选项卡栏。
    您可以使用通用 UIView 对象、您定义的自定义视图或任何其他可以缩放以填充的视图
    屏幕。

然后它有一个与我上面写的示例类似的示例。

- (void) loadView {
     //self.wantsFullScreenLayout = YES; //could add this for translucent status bars
     UIView *view = [[[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame] autorelease];
     self.view = view;
}

From Apple's View Controller Programming Guide for creating view programmatically:

  1. Create a root view object that is sized to fit the screen.
    The root view acts as the container for all other views associated with your view controller. You typically
    define the frame for this view to match the size of the application window, which itself should fill the
    screen. However, the view controller also adjusts the frame size as needed to accommodate the presence
    of assorted views, such as the system status bar, a navigation bar, or a tab bar.
    You can use a generic UIView object, a custom view you define, or any other view that can scale to fill
    the screen.

Then it has an example which is similar to the one I've written above.

凤舞天涯 2024-12-12 12:46:48

“正确”的答案可能是创建一个自定义 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.)

无语# 2024-12-12 12:46:48

我制作了一个应用程序,它加载几个 UITextViews、UIButtons 和一个 UISegment 控件。我在 viewWillAppear 中所做的,称为我所做的调整大小方法。

它以编程方式更改 UIViewController 中所有子视图的大小。这是因为 IB 中的自动调整大小效果仅此而已。

在我的 calcViewSize 函数中,我得到这样的尺寸;

CGFloat width = [[UIScreen mainScreen] applicationFrame].size.width;
CGFloat height = self.view.frame.size.height; //this will retrieve the frame we are drawn into.

然后我遍历 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;

CGFloat width = [[UIScreen mainScreen] applicationFrame].size.width;
CGFloat height = self.view.frame.size.height; //this will retrieve the frame we are drawn into.

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.

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