func setupPosition() {
box.snp.makeConstraints{(make)->Void in
make.edges.equalTo(view).inset(UIEdgeInsetsMake(64+20, 20, 250, 20))
}
textField.snp.makeConstraints{(make)->Void in
make.edges.equalTo(box).inset(UIEdgeInsetsMake(5, 5, 150, 5))
}
stackBoxOne.snp.makeConstraints{(make)->Void in
make.top.equalTo(box).offset(textField.frame.size.height)
make.left.equalTo(box).offset(5)
make.bottom.equalTo(box).offset(-90)
make.right.equalTo(box).offset(-5)
}
}
Run Code Online (Sandbox Code Playgroud)
我希望把stackBoxOne下textField。但是上面的代码不起作用。如何修改代码?感谢您的时间。
您不能textField.frame在makeConstraints闭包内使用,因为此时尚未对视图进行布局。因此,高度是0并且stackBoxOne得到 的偏移量0。
要将视图放置在另一个视图下方,请将其顶部约束连接到另一个视图的底部约束。所以在你的情况下:
stackBoxOne.snp.makeConstraints{(make)->Void in
make.top.equalTo(textField.snp.bottom)
make.left.equalTo(box).offset(5)
make.bottom.equalTo(box).offset(-90)
make.right.equalTo(box).offset(-5)
}
Run Code Online (Sandbox Code Playgroud)
除此之外,您还可以将左右约束设置为等于textFields左右约束,如下所示:
stackBoxOne.snp.makeConstraints{(make)->Void in
make.top.equalTo(textField.snp.bottom)
make.left.right.equalTo(textField)
make.bottom.equalTo(box).offset(-90)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1617 次 |
| 最近记录: |