2 developerwab developerWab 于 2013.06.03 10:37 提问

将图片对齐到屏幕底层

设置一个图片水平居中,位于屏幕的最底层。好像只要设置y坐标为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];

但不知为什么没实现? 0,0 是屏幕的左上角。所以screen_height - image_height应该对啊,请前辈解答。不胜感激

1个回答

zhanglei5415
zhanglei5415   Rxr 2013.06.03 16:19
CGSize screenSize = CGSizeMake(screenBounds.size.width * screenScale, screenBounds.size.height * screenScale);

这是何意? 屏幕的尺寸应该是这样取吧

CGRect screenBounds = [[UIScreen mainScreen] bounds];
CGSize screenSize =screenBounds.size;
NSLog(@"screen size :%@",NSStringFromCGSize(screenSize));

这样再去计算试试,如果要去掉statusbar, 还有navigationbar的话

frame.origin.y = screenSize.height- 20 - 44 - img.size.height;
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!