iphone - 如何以编程方式将图像与屏幕底部对齐?

标签 iphone ios

我正在尝试将图像水平居中放置在屏幕的最底部(无论是模拟器还是 iPhone4、iPhone5 等)。基本上我只需要将它的 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 在这里是错误的......?

最佳答案

不要使用屏幕高度,而是使用 superview View 高度:

self.view.frame.size.height

由于 subview 是根据其父 View 框架放置的,而不是屏幕的框架。

您也确实有一个逻辑错误,因为您将 ImageView 框架高度设置为 img.size.height / 2然后使用 img.size.height设置y坐标。

关于iphone - 如何以编程方式将图像与屏幕底部对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16875962/

相关文章:

iphone/ipad - 使用 Autoresize 和 addSubview 时出现问题

ios - 如何将Linphone集成到现有项目中(IOS中的SIP)

iphone - slrequest 中的应用程序 ID

ios - 如果节点具有 currentuser.uid 对象并显示在我的 UITableViewCell 中,则从 Firebase 中的节点检索对象

ios - 为什么我的 UIAlertController 没有被解雇?

iphone - xcode swift - 将文本格式化为电话号码

ios - 如何转换 .pem 到 .pfx?

iphone - 如何解决iPhone应用程序hpple HTML解析 'libxml/tree.h'文件未找到的问题?

iphone - 如何使用 MonoDevelop 和 MonoTouch 在设备上进行调试?

ios - UIAlertView: UIAlertViewStyleSecureTextInput: 数字键盘