ios CGRect 根据中心点定位

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18189572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:41:30  来源:igfitidea点击:

CGRect positioning according to center point

iosuiviewcgrectmake

提问by diogocarmo

I'm trying to create multiple views for an iPad app. First I created a menu view and then a sub-menu view with coordinates according to menu. So it looks like this:

我正在尝试为 iPad 应用程序创建多个视图。首先,我创建了一个菜单视图,然后根据菜单创建了一个带有坐标的子菜单视图。所以它看起来像这样:

enter image description here

在此处输入图片说明

What I did:

我做了什么:

self.view = [[UIView alloc] initWithFrame:CGRectMake(self.parentViewController.view.frame.size.width, 0, 150, screenHeight)];

But now on sub-menu I'm trying to create the content view, which is a UINavigationController. Trying to do the same thing, I get this result:

但是现在在子菜单上,我正在尝试创建内容视图,它是一个 UINavigationController。尝试做同样的事情,我得到了这个结果:

enter image description here

在此处输入图片说明

What I'm doing (this time creating the frame on sub-menu view controller):

我在做什么(这次在子菜单视图控制器上创建框架):

CGRect frame = CGRectMake(self.view.frame.origin.x + self.view.frame.size.width,
                            0,
                            [[UIScreen mainScreen] bounds].size.width - self.view.frame.origin.x - self.view.frame.size.width,
                            [[UIScreen mainScreen] bounds].size.height);

It's pretty self-explanatory but, I just get the sub-menu origin and add its width so I can get the right edge coordinate.

这是不言自明的,但是,我只是获取子菜单原点并添加其宽度,以便获得正确的边缘坐标。

After a lot of attempts I managed to get it working, because I noticed that the CGRectMake is using the center of the UINavigationController view to arrange its position. So the following code:

经过多次尝试,我设法让它工作,因为我注意到 CGRectMake 正在使用 UINavigationController 视图的中心来安排它的位置。所以下面的代码:

CGRect frame = CGRectMake(self.view.frame.origin.x + self.view.frame.size.width + 259,
                            0,
                            [[UIScreen mainScreen] bounds].size.width - self.view.frame.origin.x - self.view.frame.size.width,
                            [[UIScreen mainScreen] bounds].size.height);

Yields the right position.

产生正确的位置。

What's going on? I thought CGRectMake origin would always be top-left, but on this particularly view is actually top-middle (or middle-middle, not sure.) What am I doing wrong?

这是怎么回事?我认为 CGRectMake 原点总是在左上角,但在这个特别的视图中实际上是中上(或中中,不确定。)我做错了什么?

EDIT1:

编辑1:

Here's the console output for the frame with right position:

这是具有正确位置的框架的控制台输出:

enter image description here

在此处输入图片说明

Notice how the nav bar is now positioned right:

请注意导航栏现在如何正确定位:

enter image description here

在此处输入图片说明

But the x-coord is not 250 (as it should be, because menu.width + sub-menu.width = 250.)

但是 x 坐标不是 250(应该是,因为 menu.width + sub-menu.width = 250。)

EDIT2:

编辑2:

I eventually gave up. The problem was with the automatically generated UINavigationBar, which is created by the UINavigationViewController. I can't seem to figure out how to configure it. I'm gonna leave the question open in case someone knows the answer.

我最终放弃了。问题在于自动生成的 UINavigationBar,它是由 UINavigationViewController 创建的。我似乎无法弄清楚如何配置它。如果有人知道答案,我会让这个问题悬而未决。

回答by uchuugaka

Center of a CGRectis a CGPointcreated from the origin.x + ( size.width / 2 )and origin.y + ( size.height / 2 ).

a 的中心CGRectCGPointorigin.x + ( size.width / 2 )和创建的origin.y + ( size.height / 2 )

回答by Youstanzr

UIButton* btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 70, 70)];
btn.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);

This will make the button in the center no matter what the device is

无论设备是什么,这都会使按钮位于中心

回答by AlexWien

CGRectMake just creates an stucture of (x,y, witdh, height).
It your part to set the correct values.

CGRectMake 只是创建一个 (x,y, witdh, height) 的结构。
设置正确的值是您的职责。