如何更新 swift 中的锚约束
How to update anchor constraint in swift
我想在 iOS 中创建一个类似于 android 的菜单。我正在使用 布局约束 来设置 约束。我在尝试更新单击按钮时图像的左侧 constraint 时遇到了问题(它应该根据单击按钮的位置设置动画)。谁能帮我?
它应该支持横向和纵向。
我不想使用第三方代码,也不想使用 NSLayoutconstraint
。
这是我的代码。
class MenuViewController: UIViewController {
var buttons: [UIButton] = []
var imageView : UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
addMenuItems()
}
func addMenuItems() {
for _ in 0...4 {
let button : UIButton = UIButton()
self.view.addSubview(button)
button.backgroundColor = UIColor.darkGray
button.addTarget(self, action: #selector(didSelectedButton(_:)), for: .touchUpInside)
self.buttons.append(button)
}
for (index, button) in buttons.enumerated() {
button.setTitle(" \(index)", for: .normal)
if index == 0 {
button.translatesAutoresizingMaskIntoConstraints = false
button.leftAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leftAnchor, constant: 0).isActive = true
button.widthAnchor.constraint(greaterThanOrEqualToConstant: 1).isActive = true
button.heightAnchor.constraint(equalToConstant: 50).isActive = true
button.topAnchor.constraint(equalTo: self.view.topAnchor, constant:200).isActive = true
self.imageView = UIImageView()
self.view.addSubview(self.imageView)
self.imageView.backgroundColor = UIColor.red
self.imageView.translatesAutoresizingMaskIntoConstraints = false
self.imageView.leftAnchor.constraint(equalTo: button.leftAnchor, constant: 0).isActive = true
self.imageView.widthAnchor.constraint(equalTo: button.widthAnchor, multiplier: 1).isActive = true
self.imageView.heightAnchor.constraint(equalToConstant: 4).isActive = true
self.imageView.topAnchor.constraint(equalTo: self.view.topAnchor, constant:250).isActive = true
}
else {
let preButton = buttons\[index - 1\]
button.translatesAutoresizingMaskIntoConstraints = false
button.leftAnchor.constraint(equalTo: preButton.rightAnchor, constant: 0).isActive = true
button.widthAnchor.constraint(equalTo: preButton.widthAnchor, multiplier: 1).isActive = true
button.heightAnchor.constraint(equalToConstant: 50).isActive = true
button.topAnchor.constraint(equalTo: self.view.topAnchor, constant:200).isActive = true
if index == self.buttons.count - 1 {
button.rightAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.rightAnchor, constant: 0).isActive = true
}
}
}
}
@objc func didSelectedButton(_ button:UIButton) {
UIView.animate(withDuration: 1, animations: {
self.imageView.leftAnchor.constraint(equalTo: button.leftAnchor).isActive = true
self.imageView.layoutIfNeeded()
}, completion: nil)
}
}
使用锚点创建 NSLayoutContraint
s。创建新的需要停用之前的。
将此 属性 添加到您的 MenuViewController
:
var imageLeftConstraint: NSLayoutConstraint?
第一次创建约束时设置它:
imageLeftConstraint = self.imageView.leftAnchor.constraint(equalTo: button.leftAnchor, constant: 0)
imageLeftConstraint?.isActive = true
然后在添加新约束之前使用它来停用先前的约束:
@objc func didSelectedButton(_ button:UIButton) {
imageLeftConstraint?.isActive = false
imageLeftConstraint = self.imageView.leftAnchor.constraint(equalTo: button.leftAnchor)
imageLeftConstraint?.isActive = true
UIView.animate(withDuration: 1, animations: {
self.view.layoutIfNeeded()
}, completion: nil)
}
注意:你需要在imageView
(又名self.view
)的superView上调用layoutIfNeeded()
,因为[=18之间的约束=] 和 imageView
将添加到它们的共同祖先 (self.view
)。
我想在 iOS 中创建一个类似于 android 的菜单。我正在使用 布局约束 来设置 约束。我在尝试更新单击按钮时图像的左侧 constraint 时遇到了问题(它应该根据单击按钮的位置设置动画)。谁能帮我? 它应该支持横向和纵向。
我不想使用第三方代码,也不想使用 NSLayoutconstraint
。
这是我的代码。
class MenuViewController: UIViewController {
var buttons: [UIButton] = []
var imageView : UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
addMenuItems()
}
func addMenuItems() {
for _ in 0...4 {
let button : UIButton = UIButton()
self.view.addSubview(button)
button.backgroundColor = UIColor.darkGray
button.addTarget(self, action: #selector(didSelectedButton(_:)), for: .touchUpInside)
self.buttons.append(button)
}
for (index, button) in buttons.enumerated() {
button.setTitle(" \(index)", for: .normal)
if index == 0 {
button.translatesAutoresizingMaskIntoConstraints = false
button.leftAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leftAnchor, constant: 0).isActive = true
button.widthAnchor.constraint(greaterThanOrEqualToConstant: 1).isActive = true
button.heightAnchor.constraint(equalToConstant: 50).isActive = true
button.topAnchor.constraint(equalTo: self.view.topAnchor, constant:200).isActive = true
self.imageView = UIImageView()
self.view.addSubview(self.imageView)
self.imageView.backgroundColor = UIColor.red
self.imageView.translatesAutoresizingMaskIntoConstraints = false
self.imageView.leftAnchor.constraint(equalTo: button.leftAnchor, constant: 0).isActive = true
self.imageView.widthAnchor.constraint(equalTo: button.widthAnchor, multiplier: 1).isActive = true
self.imageView.heightAnchor.constraint(equalToConstant: 4).isActive = true
self.imageView.topAnchor.constraint(equalTo: self.view.topAnchor, constant:250).isActive = true
}
else {
let preButton = buttons\[index - 1\]
button.translatesAutoresizingMaskIntoConstraints = false
button.leftAnchor.constraint(equalTo: preButton.rightAnchor, constant: 0).isActive = true
button.widthAnchor.constraint(equalTo: preButton.widthAnchor, multiplier: 1).isActive = true
button.heightAnchor.constraint(equalToConstant: 50).isActive = true
button.topAnchor.constraint(equalTo: self.view.topAnchor, constant:200).isActive = true
if index == self.buttons.count - 1 {
button.rightAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.rightAnchor, constant: 0).isActive = true
}
}
}
}
@objc func didSelectedButton(_ button:UIButton) {
UIView.animate(withDuration: 1, animations: {
self.imageView.leftAnchor.constraint(equalTo: button.leftAnchor).isActive = true
self.imageView.layoutIfNeeded()
}, completion: nil)
}
}
使用锚点创建 NSLayoutContraint
s。创建新的需要停用之前的。
将此 属性 添加到您的 MenuViewController
:
var imageLeftConstraint: NSLayoutConstraint?
第一次创建约束时设置它:
imageLeftConstraint = self.imageView.leftAnchor.constraint(equalTo: button.leftAnchor, constant: 0)
imageLeftConstraint?.isActive = true
然后在添加新约束之前使用它来停用先前的约束:
@objc func didSelectedButton(_ button:UIButton) {
imageLeftConstraint?.isActive = false
imageLeftConstraint = self.imageView.leftAnchor.constraint(equalTo: button.leftAnchor)
imageLeftConstraint?.isActive = true
UIView.animate(withDuration: 1, animations: {
self.view.layoutIfNeeded()
}, completion: nil)
}
注意:你需要在imageView
(又名self.view
)的superView上调用layoutIfNeeded()
,因为[=18之间的约束=] 和 imageView
将添加到它们的共同祖先 (self.view
)。