Swift UIButton和UIView之间的约束
我是快速发展的新手,从我试图解决我面临的这个问题到现在已经有几个小时了。错误与约束有关。下面是我的代码:Swift UIButton和UIView之间的约束,swift,uiview,uibutton,nslayoutconstraint,terminate,Swift,Uiview,Uibutton,Nslayoutconstraint,Terminate,我是快速发展的新手,从我试图解决我面临的这个问题到现在已经有几个小时了。错误与约束有关。下面是我的代码: //我的约束代码 skipButtonAnchor = skipButton.anchor(view.safeAreaLayoutGuide.topAnchor, left: view.leadingAnchor, bottom: nil, right: view.trailingAnchor, topConstant: 0, leftConstant: 0, bottomConstant
//我的约束代码
skipButtonAnchor = skipButton.anchor(view.safeAreaLayoutGuide.topAnchor, left: view.leadingAnchor, bottom: nil, right: view.trailingAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 50)
bottomStackAnchor = bottomStack.anchor(nil, left: view.leadingAnchor, bottom: view.safeAreaLayoutGuide.bottomAnchor, right: view.trailingAnchor, topConstant: 0, leftConstant: 125, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 50)
//我为.anchor创建了如下函数:
func anchor(_ top: NSLayoutYAxisAnchor? = nil, left: NSLayoutXAxisAnchor? = nil, bottom: NSLayoutYAxisAnchor? = nil, right: NSLayoutXAxisAnchor? = nil, topConstant: CGFloat = 0, leftConstant: CGFloat = 0, bottomConstant: CGFloat = 0, rightConstant: CGFloat = 0, widthConstant: CGFloat = 0, heightConstant: CGFloat = 0) -> [NSLayoutConstraint] {
translatesAutoresizingMaskIntoConstraints = false
}
override func viewDidLoad() {
super.viewDidLoad()
// setup properties for skipButton and bottomStack
// ...
// add subviews to bottomStack
// ...
view.addSubview(skipButton)
view.addSubview(bottomStack)
skipButton.translatesAutoresizingMaskIntoConstraints = false
bottomStack.translatesAutoresizingMaskIntoConstraints = false
// respect safe area
let g = view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
// constrain skipButton Top / Leading / Trailing (to safe area)
skipButton.topAnchor.constraint(equalTo: g.topAnchor, constant: 0.0),
skipButton.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 0.0),
skipButton.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: 0.0),
// constrain skipButton Height = 50
skipButton.heightAnchor.constraint(equalToConstant: 50.0),
// constrain bottomStack Leading to Leading (safe-area) + 125
bottomStack.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 125.0),
// constrain bottomStack Trailing / Bottom (to safe-area)
bottomStack.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: 0.0),
bottomStack.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: 0.0),
// constrain bottomStack Height = 50
bottomStack.widthAnchor.constraint(equalToConstant: 50.0),
])
}
我期待着寻求帮助。提前感谢“我对Swift development还不熟悉”——因为您是新手,我强烈建议您使用标准约束语法,直到您了解了约束和自动布局的工作原理然后如果您觉得有帮助,请使用“助手函数”
试着这样做:
func anchor(_ top: NSLayoutYAxisAnchor? = nil, left: NSLayoutXAxisAnchor? = nil, bottom: NSLayoutYAxisAnchor? = nil, right: NSLayoutXAxisAnchor? = nil, topConstant: CGFloat = 0, leftConstant: CGFloat = 0, bottomConstant: CGFloat = 0, rightConstant: CGFloat = 0, widthConstant: CGFloat = 0, heightConstant: CGFloat = 0) -> [NSLayoutConstraint] {
translatesAutoresizingMaskIntoConstraints = false
}
override func viewDidLoad() {
super.viewDidLoad()
// setup properties for skipButton and bottomStack
// ...
// add subviews to bottomStack
// ...
view.addSubview(skipButton)
view.addSubview(bottomStack)
skipButton.translatesAutoresizingMaskIntoConstraints = false
bottomStack.translatesAutoresizingMaskIntoConstraints = false
// respect safe area
let g = view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
// constrain skipButton Top / Leading / Trailing (to safe area)
skipButton.topAnchor.constraint(equalTo: g.topAnchor, constant: 0.0),
skipButton.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 0.0),
skipButton.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: 0.0),
// constrain skipButton Height = 50
skipButton.heightAnchor.constraint(equalToConstant: 50.0),
// constrain bottomStack Leading to Leading (safe-area) + 125
bottomStack.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 125.0),
// constrain bottomStack Trailing / Bottom (to safe-area)
bottomStack.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: 0.0),
bottomStack.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: 0.0),
// constrain bottomStack Height = 50
bottomStack.widthAnchor.constraint(equalToConstant: 50.0),
])
}
“我面临的问题”是什么?谢谢你的帮助!:)