iOS UIImageView Position
I need to put an image to the bottom of screen, so I used UIImageView and tried two ways to specify its position:
someImageView.frame=开发者_C百科CGRectMake(x, y, a, b);
[someImageView initWithFrame:CGRectMake(x, y, a, b)];
But it shows up at the top covering the navigation bar. What did I do wrong? What would you suggest to do?
Thank you in advance.
Are you sure that you are assuming the correct screen coordinates? The origin of the screen is in the top-left, not the bottom-left. Try adding an object in Interface Builder and inspecting its frame in there.
I am a little confused by the second line in your code snippet above. Are you calling initWithFrame:
on an object that is already initialised? If so, you might see some unexpected behaviour. Objects should be declared:
UIImageView *someImageView = [[UIImageView alloc] initWithFrame:frame];
Rather than:
UIImageView *someImageView = [UIImageView alloc];
[someImageView initWithFrame:frame];
Always keep alloc
and init
on the same line of code.
Remember that top-left of the screen is (0,0), while bottom-right is (320,460). You may use
someImageView.frame=CGRectMake(320 - a, 460 - b, a, b);
assuming that a
is the width and b
is the height
Your code is correct! Check the values you are using in CGRectMake
. For instance:
someImageView.frame = CGRectMake(0,
[UIScreen mainScreen].bounds.size.height-ivHeight,
[UIScreen mainScreen].bounds.size.width,
ivHeight);
Your (x,y,a,b) could be the problem. Make sure the y is > 0. Also, make sure that you are adding the frame to the correct subview. Adding it to the wrong one could cause the problem. If you are still having trouble, post the code that you are using and I could be more helpful.
Also,Do not call initWithFrame without calling alloc first. It should be:
someImageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, a, b)];
精彩评论