xcode 在代码中设置时,在 Interface Builder 中设置 UIImageView 相对于父视图的框架位置是不同的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5311639/
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-09-14 20:30:49  来源:igfitidea点击:

Setting an UIImageView's frame position relative to parent view in Interface Builder is different when set in code?

objective-ccocoa-touchxcodeiosxcode4

提问by Camsoft

I have a view controller and a view. I then have a UIImageView created through IB and position using the IB Inspector. Setting the UIImageView's frame to x and y coordinatates to 0 results in the image being placed top left of the view, below the status bar.

我有一个视图控制器和一个视图。然后我通过 IB 创建了一个 UIImageView 并使用 IB Inspector 定位。将 UIImageView 的框架设置为 x 和 y 坐标为 0 会导致图像被放置在视图的左上角,状态栏下方。

If I then implement viewDidLoad method of the view controller and set UIImageView 's frame position using:

如果我然后实现视图控制器的 viewDidLoad 方法并使用以下方法设置 UIImageView 的帧位置:

myimageview.frame = CGRectMake(0, 0, 50, 50);

Which are exactly the same coordinates as defined for the UIImageView in IB, the image is moved up underneath the status bar. It seems the coordiantes when set via code are realtive to the Window and not the parent View as shown IB.

与为 IB 中的 UIImageView 定义的坐标完全相同,图像在状态栏下方向上移动。通过代码设置时的坐标似乎与窗口相关,而不是父视图,如图 IB 所示。

Anyone know why there is a decrepancy between IB and code?

有人知道为什么IB和代码之间存在差异吗?



Some useful debugging info:

一些有用的调试信息:

Note: That width and height of the Image View is in fact 35x37. The above dimensions were just to simplify the orignal question.

注意:Image View 的宽度和高度实际上是 35x37。以上尺寸只是为了简化原始问题。

The frame info before setting it in code:

在代码中设置之前的帧信息:

2011-03-15 13:15:34.249 MyAPP [98288:207] {{0, 49}, {35, 37}}

The frame info after setting it in code:

代码中设置后的帧信息:

2011-03-15 13:15:34.250 MyAPP [98288:207] {{0, 0}, {35, 37}}

As defined in Interface Builder:

如接口生成器中所定义:

Interface Builder

界面生成器

As you can see IB says that the UIImageView X and Y coordinators are 0 ,0. While the debugging info above says that it's actually 0, 49.

如您所见,IB 表示 UIImageView X 和 Y 协调器为 0 ,0。虽然上面的调试信息说它实际上是 0、49。

回答by Erik B

I never use the Interface builder, so this answer might be based on some false assumptions, but hopefully my assumtpions are correct.

我从不使用界面构建器,所以这个答案可能基于一些错误的假设,但希望我的假设是正确的。

I assume that before

我假设之前

myimageview.frame = CGRectMake(0, 0, 50, 50);

myimageview.framewill be set to whatever you set in the interface builder. I would suggest executing the following lines just to see how you're changing the frameproperty:

myimageview.frame将设置为您在界面构建器中设置的任何内容。我建议执行以下几行只是为了查看您如何更改frame属性:

NSLog(@"%@", NSStringFromCGRect(myimageview.frame));
myimageview.frame = CGRectMake(0, 0, 50, 50);
NSLog(@"%@", NSStringFromCGRect(myimageview.frame));

That should help you figure out the difference.

这应该可以帮助您找出差异。

Btw, the coordinates are always relative to the parent view.

顺便说一句,坐标总是相对于父视图。

回答by Camsoft

It seems that the problem I was having was due to a bug. I've rebuilt the UI in Interface Builder and now it works as expected.

看来我遇到的问题是由于错误造成的。我已经在 Interface Builder 中重建了 UI,现在它按预期工作。

回答by Brian

The frame is relative to the parent object. So 0,0 will put you at the top left of the parent view. If that view is your main view then you will be right under the status bar. If you already have this set in IB why do you need to reposition it? For what you are doing you may want to use boundsinstead since it is in the space of the object itself. I don't use IB much, but it may be that you are setting the bounds.

框架是相对于父对象的。所以 0,0 会让你在父视图的左上角。如果该视图是您的主视图,那么您将位于状态栏下方。如果您已经在 IB 中设置了此设置,为什么需要重新定位它?对于您正在做的事情,您可能想要使用bounds它,因为它位于对象本身的空间中。我不经常使用 IB,但可能是您设置了界限。