如何并排添加两个 UIButton?
How can I add two UIButtons side by side?
每当我模拟我的应用程序时,UIButtons 都不会出现。我收到一条错误消息
Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to activate constraint with anchors
and
because they have no common ancestor. Does the constraint or its
anchors reference items in different view hierarchies? That's
illegal.'
我该如何解决这个问题?
func setView(){
signInButton.addTarget(self, action: #selector(userSignIn), for: .touchUpInside)
signInButton.heightAnchor.constraint(equalToConstant: 120).isActive = true
signInButton.widthAnchor.constraint(equalToConstant: 120).isActive = true
signInButton.setTitle("Sign in",for: .normal)
createAnAccountButton.addTarget(self, action: #selector(createAnAccount), for: .touchUpInside)
createAnAccountButton.setTitle("Create an Account", for: .normal)
createAnAccountButton.heightAnchor.constraint(equalToConstant: 120).isActive = true
createAnAccountButton.widthAnchor.constraint(equalToConstant: 120).isActive = true
let buttonStack = UIStackView()
buttonStack.axis = NSLayoutConstraint.Axis.vertical
buttonStack.distribution = UIStackView.Distribution.equalSpacing
buttonStack.alignment = UIStackView.Alignment.center
buttonStack.spacing = 16
buttonStack.addArrangedSubview(signInButton)
buttonStack.addArrangedSubview(createAnAccountButton)
buttonStack.translatesAutoresizingMaskIntoConstraints = false
buttonStack.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
buttonStack.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
buttonStack.frame = CGRect(x: 0, y: 0, width: 100, height: 60)
self.view.addSubview(buttonStack)
// let barButtonSignIn = UIBarButtonItem(customView: signInButton)
// self.navigationItem.leftBarButtonItem = barButtonSignIn
}
您必须在激活约束之前调用 addSubview
。
每当我模拟我的应用程序时,UIButtons 都不会出现。我收到一条错误消息
Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to activate constraint with anchors and because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies? That's illegal.'
我该如何解决这个问题?
func setView(){
signInButton.addTarget(self, action: #selector(userSignIn), for: .touchUpInside)
signInButton.heightAnchor.constraint(equalToConstant: 120).isActive = true
signInButton.widthAnchor.constraint(equalToConstant: 120).isActive = true
signInButton.setTitle("Sign in",for: .normal)
createAnAccountButton.addTarget(self, action: #selector(createAnAccount), for: .touchUpInside)
createAnAccountButton.setTitle("Create an Account", for: .normal)
createAnAccountButton.heightAnchor.constraint(equalToConstant: 120).isActive = true
createAnAccountButton.widthAnchor.constraint(equalToConstant: 120).isActive = true
let buttonStack = UIStackView()
buttonStack.axis = NSLayoutConstraint.Axis.vertical
buttonStack.distribution = UIStackView.Distribution.equalSpacing
buttonStack.alignment = UIStackView.Alignment.center
buttonStack.spacing = 16
buttonStack.addArrangedSubview(signInButton)
buttonStack.addArrangedSubview(createAnAccountButton)
buttonStack.translatesAutoresizingMaskIntoConstraints = false
buttonStack.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
buttonStack.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
buttonStack.frame = CGRect(x: 0, y: 0, width: 100, height: 60)
self.view.addSubview(buttonStack)
// let barButtonSignIn = UIBarButtonItem(customView: signInButton)
// self.navigationItem.leftBarButtonItem = barButtonSignIn
}
您必须在激活约束之前调用 addSubview
。