简体   繁体   English

在iOS布局(或快速)中对齐和锚定图像

[英]Align and anchor images in iOS layout (or swift)

I have two images and I would like to align horizontally, but the second image, but the second image must be aligned and anchored at the bottom of the first image, I post a picture to understand it better. 我有两个图像,我想水平对齐,但第二个图像,但第二个图像必须对齐并锚定在第一个图像的底部,我发布图片以更好地理解它。

Picture here 想象一下

I think there isn't method to do so via the xcode editor. 我认为没有办法通过xcode编辑器这样做。

Thanks guys! 多谢你们!

Edit: (I accidentally deleted my comment and I can not comment) 编辑:(我不小心删除了我的评论,我无法发表评论)

@Rory McKinnel I did it thanks, now the problem is when the screen has a different size, because the images get bigger but I can not change the offset value setted in the layout editor of xcode. @Rory McKinnel我做到了谢谢,现在的问题是当屏幕有不同的大小时,因为图像变大但我无法改变xcode布局编辑器中设置的偏移值。 I have do this: 我这样做了:

class TopRow: UITableViewCell { 
   @IBOutlet weak var imgTop: UIImageView!
   @IBOutlet weak var OffsetWhiteCircle: NSLayoutConstraint!
   @IBOutlet weak var imgWhiteCircle: UIImageView!  
   override func awakeFromNib() {
        super.awakeFromNib()  
        let heightImgWhite = imgWhiteCircle.frame.size.height
        let div = Float(heightImgWhite / -2)
        OffsetWhiteCircle.constant = CGFloat(div)
} 

Align image 2 with image 1 centres horizontally. 将图像2与图像1水平对齐。

Anchor the top edge of image 2 to be the bottom edge of image 1 with an offset equal to half the image 2 height. 将图像2的上边缘锚定为图像1的下边缘,偏移量等于图像2高度的一半。 This will make image 2 show half way up from the bottom of image 1. 这将使图像2从图像1的底部向上显示一半。

That should give you what you have in the diagram. 这应该会给你图中的内容。

This relies on image 2 always having the same height. 这依赖于图像2始终具有相同的高度。 If the height of image 2 can change, you could link the offset constraint to an IBOutlet and set the offset in your code when you know the image size. 如果图像2的高度可以更改,则可以将偏移约束链接到IBOutlet,并在知道图像大小时在代码中设置偏移。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM