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

查看:103
本文介绍了如何获取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天全站免登陆