开发者

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

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.

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

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

Which are exactly the same coo开发者_JAVA技巧rdinates 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.

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


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.

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:

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

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.


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.frame will 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 frame property:

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.


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 bounds instead 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜