Swift 顶锚0在中间显示

Swift 顶锚0在中间显示,swift,xcode,Swift,Xcode,我已经在我的Xcode项目中安装了LBTAComponents框架。现在我想将我的按钮放在页面顶部,下面是我的代码: button.anchor(view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0

我已经在我的Xcode项目中安装了
LBTAComponents
框架。现在我想将我的按钮放在页面顶部,下面是我的代码:

button.anchor(view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 0)`.

这个代码的悲哀问题是它显示了我在页面中间而不是在顶峰的光荣按钮。如何使我的按钮显示在页面顶部

想一想你做了什么:你把按钮的四个面(左、右、上、下)都钉在视图的四个面上了——你怎么能期望按钮像你想的那样位于顶部呢

您应该在底部参数中使用
nil
,并在最后一个参数中设置按钮高度-高度常数应该可以工作,但如果不需要按钮的特定值,则无需设置最后一个参数的高度:

let button = UIButton(type: .system)
button.setTitle("Hello", for: .normal)
button.backgroundColor = .red
view.addSubview(button)
button.anchor(view.topAnchor, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 0)
结果: