我有一个通过编程添加的
如何解决这个问题呢?
UIImageView
和一个在Storyboard中添加的按钮。现在我需要在它们之间添加“垂直间隔”,但我不知道该怎么做。如果我在故事板中添加了UIImageView
,那么这将很容易解决:
![enter image description here](https://istack.dev59.com/qnoMC.webp)
UIImageView
和一个在Storyboard中添加的按钮。现在我需要在它们之间添加“垂直间隔”,但我不知道该怎么做。如果我在故事板中添加了UIImageView
,那么这将很容易解决:
假设您的UIImageView
添加在顶部,就像您在上面的图像中所示,那么您可以以以下方式通过编程方式添加约束:
override func viewDidLoad() {
super.viewDidLoad()
// assuming here you have added the self.imageView to the main view and it was declared before.
self.imageView.setTranslatesAutoresizingMaskIntoConstraints(false)
// create the constraints with the constant value you want.
var verticalSpace = NSLayoutConstraint(item: self.imageView, attribute: .Bottom, relatedBy: .Equal, toItem: self.button, attribute: .Bottom, multiplier: 1, constant: 50)
// activate the constraints
NSLayoutConstraint.activateConstraints([verticalSpace])
}
attribute: .Top
? - Liumx31UIImageView
相对于 UIButton
位置靠后,如果你想要改变它,那么你需要交换它们的角色。 - Victor Siglerlet verticalSpace = NSLayoutConstraint(item: view1, attribute: .top, relatedBy: .equal, toItem: view2, attribute: .bottom, multiplier: 1, constant: 0)
NSLayoutConstraint.activate([verticalSpace])
注意:为了使此功能正常工作,您必须添加水平位置。