如何获取 CGRect 的 x 和 y 以使用框架初始化控件? [英] How to get an x and a y for a CGRect to initialize a control with a frame?

查看:16
本文介绍了如何获取 CGRect 的 x 和 y 以使用框架初始化控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 iPhone 应用程序,我想知道程序员如何在不使用 Interface Builder 的情况下正确定位控件.或多或少,这是因为我想在不使用 Interface Builder 的情况下定位几个控件.

I'm programming an iPhone app and I'm wondering how programmers properly position controls without using Interface Builder. More or less, this is because I want to position a couple of controls without using Interface Builder.

谢谢!

推荐答案

尝试使用 IB 查看您想要放置对象的坐标,然后将它们添加到代码中.您通常需要进行一些试验和错误才能使其正确.

Try using the IB to see the coordinates at which you would like to position your objects and then add them in code. You will usually need to do some trial and error to get it right.

您可能还希望相对于屏幕大小来定位项目.在这种情况下,我建议避免在应用程序中像 480/2 这样对屏幕大小进行硬编码.相反,使用 UIScreen 的共享对象来获取当前屏幕的大小,如下所示:

You might also want to position items relative to the size of the screen. In this case I would suggest avoiding hardcoding the size of the screen in the app like 480/2. Instead, use the UIScreen's shared object to get the current screen's size like this:

(将 UIView 放置在屏幕中心的示例 - 任何屏幕、iPhone、iPad、iPad 外部屏幕)

(example to position a UIView in the center of the screen - any screen, iPhone, iPad, iPad external screen)

myView.center = CGPointMake([[UIScreen mainScreen] bounds].size.width/2, [[UIScreen mainScreen] bounds].size.height/2);

这篇关于如何获取 CGRect 的 x 和 y 以使用框架初始化控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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