Reputation: 9742
I am trying to place an image horizontally centered at the very bottom of the screen (regardless of if it's the simulator, or an iPhone4, iPhone5, etc). Basically I should just need to set its y to screen_height - image_height.
UIImage *img = [UIImage imageNamed:@"my-image.png"];
UIImageView *imgView = [[UIImageView alloc]initWithImage:img];
imgView.frame = CGRectMake(0, 0, img.size.width / 2, img.size.height / 2);
CGRect screenBounds = [[UIScreen mainScreen] bounds];
CGFloat screenScale = [[UIScreen mainScreen] scale];
CGSize screenSize = CGSizeMake(screenBounds.size.width * screenScale, screenBounds.size.height * screenScale);
CGRect frame = imgView.frame;
frame.origin.x = 0;
frame.origin.y = screenSize.height - img.size.height;
imgView.frame = frame;
[self.view addSubview:imgView];
What am I doing wrong? 0,0 is the top left of the screen, so I don't understand why screen_height - image_height is wrong here... ?
Upvotes: 1
Views: 3437
Reputation: 9152
If you really, really want to force it to the bottom of the screen:
CGRect screenBounds = [UIScreen mainScreen].bounds;
CGRect viewFrameOnScreen = [[view superview] convertRect:view.frame toView:nil];
viewFrameOnScreen.origin.y = screenBounds.size.height - viewFrameOnScreen.size.height;
view.frame = [[view superview] convertRect:viewFrameOnScreen fromView:nil];
But I doubt you want to unconditionally force it to the bottom of the screen, even if there is a tab bar or something there. Except in unusual circumstances, views shouldn't venture outside the bounds of their parent view. If you just want it at the bottom of the parent view, then
view.frame.origin.y = [view superview].bounds.size.height - view.frame.size.height;
Upvotes: 0
Reputation: 1554
Align the subview at the bottom of the screen and then use an autoresizing mask to make sure the bottom margin stays constant at 0. This will keep the subview aligned at the bottom of its superview even if the superview's frame changes.
e.g.
UIImage *img = [UIImage imageNamed:@"my-image.png"];
UIImageView *imgView = [[UIImageView alloc]initWithImage:img];
imgView.frame = CGRectMake(0, 0, img.size.width, img.size.height);
CGRect frame = imgView.frame;
frame.origin.x = 0;
frame.origin.y = self.view.frame.size.height - img.size.height;
imgView.frame = frame;
imgView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
Upvotes: 1
Reputation: 874
Make sure you calculate those position metrics in -viewWillAppear
when the frame of the view controller's view is properly calculated. In -viewDidLoad
the frame corresponds to the metrics loaded from the NIB and if your XIB is configured for 3,5" display the view will be higher on 4" displays (iPhone 5)
Upvotes: 1
Reputation: 119031
Instead of using the screen height, use the superview view height:
self.view.frame.size.height
As the subview is placed in terms of its superview frame, not the frame of the screen.
You do also have a logical error, because you're setting the image view frame height to img.size.height / 2
and then later using the img.size.height
to set the y coordinate.
Upvotes: 2