开发者

Full-screen UIImageView aligning to bottom

With the following code:

UIImageView *largeImageView = [[UIImageView alloc] initWithImage:theImage];
[largeImageView setContentMode:UIViewContentModeScaleAspectFit];
largeImageView.frame = [[UIScreen mainScreen] applicationFrame];

[viewController.view addSubview:largeImageView];
viewController.hidesBottomBarWhenPushed = YES;
[self.navigationCo开发者_Python百科ntroller pushViewController:viewController animated:YES];

I would expect the image to be at the top of the View, not the bottom. I double-checked the origin x and y, and they are 0,0.

Here is a screenshot: http://cl.ly/8F3J


Thanks to Tommy for providing some thinking out loud and debugging help, I figured out what I was doing wrong. I changed around the order of operations and added the imageview as a subview after I pushed the viewcontroller on the nav stack. This fixed the issue as my view controller had it's new view from the nav controller.


largeImageView is a subview of viewController.view, so its coordinates are relative to that of its superview. Probably you want something more like:

// the bounds of viewController.view contain its correct size, but
// have an origin of (0, 0)
largeImageView.frame = viewController.view.bounds;

[viewController.view addSubview:largeImageView];

What's probably happening at the minute is that you're getting a frame much larger than the view controller's view size, then the fact that UIViewContentModeScaleAspectFit will be adding some space at the top and bottom of the view as necessary (assuming your image is proportionally wider than the target view) is pushing the image off the bottom of the screen.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜