Ios 具有固定宽度的定心按钮断开NSLayoutConstraint
我有一个有趣的问题。我的应用程序完全是通过代码而不是故事板来管理的。我有一个UIViewController,它是以编程方式显示的。它为它创建一个按钮和约束 这就是控制器代码的样子Ios 具有固定宽度的定心按钮断开NSLayoutConstraint,ios,swift,autolayout,nslayoutconstraint,ios-autolayout,Ios,Swift,Autolayout,Nslayoutconstraint,Ios Autolayout,我有一个有趣的问题。我的应用程序完全是通过代码而不是故事板来管理的。我有一个UIViewController,它是以编程方式显示的。它为它创建一个按钮和约束 这就是控制器代码的样子 import Foundation import UIKit class CreateViewController: UIViewController { lazy var button: UIButton = { let button = UIButton() butt
import Foundation
import UIKit
class CreateViewController: UIViewController {
lazy var button: UIButton = {
let button = UIButton()
button.layer.bounds = CGRect(x: 0, y: 0, width: 50, height: 50)
button.translatesAutoresizingMaskIntoConstraints = false
button.backgroundColor = .green
return button
}()
func setupViews() {
self.view.addSubview(button)
}
func setupConstraints() {
self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[button]-100-|", options: NSLayoutConstraint.FormatOptions(rawValue: 0), metrics: nil, views: ["button": button]))
self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-[button(50)]-|", options: NSLayoutConstraint.FormatOptions(rawValue: 0), metrics: nil, views: ["button": button]))
}
override func viewDidLoad() {
setupViews()
setupConstraints()
}
}
它抛出了这个错误
(
"<NSLayoutConstraint:0x60000288bb60 UIButton:0x7fa1ab422680.leading == UILayoutGuide:0x6000032da760'UIViewLayoutMarginsGuide'.leading NSSpace(0) (active)>",
"<NSLayoutConstraint:0x60000288bb10 UIButton:0x7fa1ab422680.width == 50 (active)>",
"<NSLayoutConstraint:0x60000288d7c0 UILayoutGuide:0x6000032da760'UIViewLayoutMarginsGuide'.trailing == UIButton:0x7fa1ab422680.trailing NSSpace(0) (active)>",
"<NSLayoutConstraint:0x6000028fe990 'UIView-Encapsulated-Layout-Width' UIView:0x7fa1ab4224a0.width == 375 (active)>",
"<NSLayoutConstraint:0x60000288fe30 'UIView-leftMargin-guide-constraint' H:|-(16)-[UILayoutGuide:0x6000032da760'UIViewLayoutMarginsGuide'](LTR) (active, names: '|':UIView:0x7fa1ab4224a0 )>",
"<NSLayoutConstraint:0x60000288d180 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x6000032da760'UIViewLayoutMarginsGuide']-(16)-|(LTR) (active, names: '|':UIView:0x7fa1ab4224a0 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x60000288d7c0 UILayoutGuide:0x6000032da760'UIViewLayoutMarginsGuide'.trailing == UIButton:0x7fa1ab422680.trailing NSSpace(0) (active)>
如果您能帮助理解这些约束被违反的原因,我们将不胜感激!如果问题的任何部分不清楚,请告诉我
仅供参考,生成的视图如下所示:
您可以尝试以下方法来添加更快速、更清晰的约束
func setupConstraints() {
let topConstraint = NSLayoutConstraint(item: button,
attribute: .top,
relatedBy: .equal,
toItem: self.view,
attribute: .top,
multiplier: 1,
constant: 100)
let bottomConstraint = NSLayoutConstraint(item: button,
attribute: .left,
relatedBy: .equal,
toItem: self.view,
attribute: .left,
multiplier: 1,
constant: 100)
self.view.addConstraints([topConstraint,bottomConstraint])
}
虽然我想知道为什么您要向按钮添加约束,但您可以在初始化时声明x和y。如果你需要帮助,我也在按钮的中间添加了代码,希望能有所帮助
lazy var button: UIButton = {
let button = UIButton()
button.frame = CGRect(x: UIScreen.main.bounds.width/2-25, y: UIScreen.main.bounds.height/2-25, width: 50, height: 50)
button.backgroundColor = .green
return button
}()
您可以尝试以下方法来添加约束,这将更加快速和干净
func setupConstraints() {
let topConstraint = NSLayoutConstraint(item: button,
attribute: .top,
relatedBy: .equal,
toItem: self.view,
attribute: .top,
multiplier: 1,
constant: 100)
let bottomConstraint = NSLayoutConstraint(item: button,
attribute: .left,
relatedBy: .equal,
toItem: self.view,
attribute: .left,
multiplier: 1,
constant: 100)
self.view.addConstraints([topConstraint,bottomConstraint])
}
虽然我想知道为什么您要向按钮添加约束,但您可以在初始化时声明x和y。如果你需要帮助,我也在按钮的中间添加了代码,希望能有所帮助
lazy var button: UIButton = {
let button = UIButton()
button.frame = CGRect(x: UIScreen.main.bounds.width/2-25, y: UIScreen.main.bounds.height/2-25, width: 50, height: 50)
button.backgroundColor = .green
return button
}()
用可视化格式语言将元素居中是困难的 您可以使用
.centerXAnchor
轻松地完成此操作:
class CreateViewController: UIViewController {
lazy var button: UIButton = {
let button = UIButton()
// next line is not needed
// button.layer.bounds = CGRect(x: 0, y: 0, width: 50, height: 50)
button.translatesAutoresizingMaskIntoConstraints = false
button.backgroundColor = .green
return button
}()
func setupViews() {
self.view.addSubview(button)
}
func setupConstraints() {
NSLayoutConstraint.activate([
// button width of 50
button.widthAnchor.constraint(equalToConstant: 50.0),
// button height of 50
button.heightAnchor.constraint(equalToConstant: 50.0),
// bottom of button 100-pts from bottom of view
// note: contant is negative!
button.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -100),
// center the button horizontally
button.centerXAnchor.constraint(equalTo: view.centerXAnchor),
])
}
override func viewDidLoad() {
setupViews()
setupConstraints()
}
}
用可视化格式语言将元素居中是困难的 您可以使用
.centerXAnchor
轻松地完成此操作:
class CreateViewController: UIViewController {
lazy var button: UIButton = {
let button = UIButton()
// next line is not needed
// button.layer.bounds = CGRect(x: 0, y: 0, width: 50, height: 50)
button.translatesAutoresizingMaskIntoConstraints = false
button.backgroundColor = .green
return button
}()
func setupViews() {
self.view.addSubview(button)
}
func setupConstraints() {
NSLayoutConstraint.activate([
// button width of 50
button.widthAnchor.constraint(equalToConstant: 50.0),
// button height of 50
button.heightAnchor.constraint(equalToConstant: 50.0),
// bottom of button 100-pts from bottom of view
// note: contant is negative!
button.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -100),
// center the button horizontally
button.centerXAnchor.constraint(equalTo: view.centerXAnchor),
])
}
override func viewDidLoad() {
setupViews()
setupConstraints()
}
}