How to get an x and a y for a CGRect to initialize a control with a frame?

纵然是瞬间 提交于 2019-12-01 23:05:48

问题


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.

Thanks!


回答1:


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.

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:

(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);



回答2:


If you don't want use IB, there is no miracle solution. You must keep in mind the dimension of the iPod/iPhone (320px*480px).

In general you have to center your GUI component in the x axis and then adjust your component on the y axis.




回答3:


I do all my controls and views programmatically, and its not difficult at all. I use IB to mockup my app, so I just find where I want it there and then put that into the code itself.



来源:https://stackoverflow.com/questions/2315169/how-to-get-an-x-and-a-y-for-a-cgrect-to-initialize-a-control-with-a-frame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!