Objective-C 对象放置 iPad

发布于 2024-11-13 10:50:40 字数 311 浏览 3 评论 0原文

我有一个扩展 UIViewController 的 GUI 类。

在其函数 viewDidLoad 中,我希望有一个 UITextField 实例。我希望该实例不与简单的 CGRect 对齐,设置其偏移量以及宽度和高度,但我希望动态调整它的大小,使其填充屏幕的整个宽度,从顶部、左侧和底部偏移 5px每个右侧,高度为 20px。这是怎么做到的?

无论设备方向如何,它都应该保持“相对绝对”定位,支持所有方向。

也许有类似 LayoutManager 的东西,例如 Java 中的 GridBagLayout?

I have a GUI class extending UIViewController.

In its function viewDidLoad, I would like to have a UITextField instance. I would like that instance not aligned as a simple CGRect, setting its offset and width and height, but I want it rather to be resized dynamically, making it fill the whole width of the screen, with an offset of 5px from top, left and right each and a height of 20px. How is that done?

It should maintain the "relatively-absolute" positioning regardless of the device orientation, all orientations being supported.

Maybe there's something like a LayoutManager, as, for instance, GridBagLayout in Java?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

安稳善良 2024-11-20 10:50:41

您想要设置视图的 autoresizingMask 属性。

UIView *theParentView; // Assume this is the root view that owns the whole screen

CGRect textFieldFrame = CGRectMake(5, // Top-left corner x position
                                   5, // Top-left corner y position
                                   [theParentView bounds].width - 10, // Width
                                   20); // Height

UIView *myView = [[UIView alloc] initWithFrame:textFieldFrame];

// This will make it resize with the parent view, maintaining margins
[myView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];

[theParentView addSubview:myView];

[myView release];

这一切也可以通过 Interface Builder 完成。甚至还有一个小动画显示视图如何在 Interface Builder 中调整大小。

You want to set the autoresizingMask property of the view.

UIView *theParentView; // Assume this is the root view that owns the whole screen

CGRect textFieldFrame = CGRectMake(5, // Top-left corner x position
                                   5, // Top-left corner y position
                                   [theParentView bounds].width - 10, // Width
                                   20); // Height

UIView *myView = [[UIView alloc] initWithFrame:textFieldFrame];

// This will make it resize with the parent view, maintaining margins
[myView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];

[theParentView addSubview:myView];

[myView release];

This can all be done from Interface Builder as well. There's even a little animation showing how the view will resize in Interface Builder.

临风闻羌笛 2024-11-20 10:50:41

请记住,UIViewController 本身并不显示,但它管理视图对象的层次结构。您可以在 -viewDidLoad 中创建文本字段,但需要将其添加到视图控制器的视图中才能看到它。您可以这样做:

CGFloat inset = 5.0;
CGFloat fieldHeight = 20.0;
CGRect fieldRect = CGRectMake(inset,
                              inset,
                              self.view.size.width - (2 * inset),
                              fieldHeight);
UITextField *field = [[UITextField alloc] initWithFrame:fieldRect];
field.autoResizingMask = UIViewAutoresizingFlexibleWidth;
[self.view addSubview:field];
[field release];

当然,如果您稍后需要再次引用该字段,您还需要将指向它的指针存储在属性或 ivar 中。

Remember that a UIViewController itself isn't displayed, but it manages a hierarchy of view objects. You can create the text field in -viewDidLoad, but it needs to be added to the view controller's view before you can see it. You'd do something like this:

CGFloat inset = 5.0;
CGFloat fieldHeight = 20.0;
CGRect fieldRect = CGRectMake(inset,
                              inset,
                              self.view.size.width - (2 * inset),
                              fieldHeight);
UITextField *field = [[UITextField alloc] initWithFrame:fieldRect];
field.autoResizingMask = UIViewAutoresizingFlexibleWidth;
[self.view addSubview:field];
[field release];

Of course, if you need to refer to that field again later, you'll also want to stash a pointer to it in a property or ivar.

飘逸的'云 2024-11-20 10:50:41

操作系统中没有内置任何此类内容。我见过几个版本的类似你所问的内容,包括我自己的版本,位于 GitHub

There is nothing as such built into the OS. I have seen a couple of versions of something like what you're asking, including my own which is up on GitHub

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