在 swift 5 中更新约束
Updating constraints in swift 5
我试过停用和重新激活...但是我似乎无法找到如何去做...我有代码可以用作示例但它总是让我失败,如果有人知道如何更新约束在 swift 5 请让我知道...我知道 updateConstraint() 存在只是不确定如何使用它。
let constraint1: NSLayoutConstraint? = text.bottomAnchor.constraint(equalTo: view.bottomAnchor)
let constraint2: NSLayoutConstraint? = text.bottomAnchor.constraint (equalTo: view.bottomAnchor, constant: 5)
if acondition {
constraint1?.isActive = false
constraint2.isActive = true
} else {
constraint2.isActive = false
constraint1.isActive = true
}
也许你可以检查你的 @properties
并将 weak
替换为 strong
。原因是,isActive = false
将设置 self.yourConstraint = nil
,因此您将无法再次使用 self.yourConstraint
。
首先声明变量,将启动约束变量设置为 true 并调用激活和禁用约束的函数后,尝试使用我的示例:
在控制器下 class 声明一个按钮和一个要移动的视图:
let dummyView = UIView()
let button = UIButton(type: .system)
在声明变量后以约束移动视图:
var up: NSLayoutConstraint?
var down: NSLayoutConstraint?
在 ViewDiLoad 中像这样设置视图、按钮和约束:
dummyView.backgroundColor = .yellow
dummyView.translatesAutoresizingMaskIntoConstraints = false
button.setTitle("viewDiwn", for: .normal)
button.backgroundColor = .red
button.setTitleColor(.white, for: .normal)
button.titleLabel?.font = .systemFont(ofSize: 16)
button.addTarget(self, action: #selector(handletapButton), for: .touchUpInside)// button action that call handletapButton func
button.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(dummyView)
up = dummyView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor)
up?.isActive = true // start constraint active
down = dummyView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 100) // constraint to activate for move the view
dummyView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
dummyView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
dummyView.heightAnchor.constraint(equalToConstant: 50).isActive = true
view.addSubview(button)
button.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
button.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
button.heightAnchor.constraint(equalToConstant: 50).isActive = true
button.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
现在编写通过约束动画移动视图的函数:
@objc func handletapButton() {
UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseOut, animations: {
self.up?.isActive = false
self.down?.isActive = true
self.view.layoutIfNeeded()
}, completion: nil)
}
这是结果
我试过停用和重新激活...但是我似乎无法找到如何去做...我有代码可以用作示例但它总是让我失败,如果有人知道如何更新约束在 swift 5 请让我知道...我知道 updateConstraint() 存在只是不确定如何使用它。
let constraint1: NSLayoutConstraint? = text.bottomAnchor.constraint(equalTo: view.bottomAnchor)
let constraint2: NSLayoutConstraint? = text.bottomAnchor.constraint (equalTo: view.bottomAnchor, constant: 5)
if acondition {
constraint1?.isActive = false
constraint2.isActive = true
} else {
constraint2.isActive = false
constraint1.isActive = true
}
也许你可以检查你的 @properties
并将 weak
替换为 strong
。原因是,isActive = false
将设置 self.yourConstraint = nil
,因此您将无法再次使用 self.yourConstraint
。
首先声明变量,将启动约束变量设置为 true 并调用激活和禁用约束的函数后,尝试使用我的示例:
在控制器下 class 声明一个按钮和一个要移动的视图:
let dummyView = UIView()
let button = UIButton(type: .system)
在声明变量后以约束移动视图:
var up: NSLayoutConstraint?
var down: NSLayoutConstraint?
在 ViewDiLoad 中像这样设置视图、按钮和约束:
dummyView.backgroundColor = .yellow
dummyView.translatesAutoresizingMaskIntoConstraints = false
button.setTitle("viewDiwn", for: .normal)
button.backgroundColor = .red
button.setTitleColor(.white, for: .normal)
button.titleLabel?.font = .systemFont(ofSize: 16)
button.addTarget(self, action: #selector(handletapButton), for: .touchUpInside)// button action that call handletapButton func
button.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(dummyView)
up = dummyView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor)
up?.isActive = true // start constraint active
down = dummyView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 100) // constraint to activate for move the view
dummyView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
dummyView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
dummyView.heightAnchor.constraint(equalToConstant: 50).isActive = true
view.addSubview(button)
button.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
button.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
button.heightAnchor.constraint(equalToConstant: 50).isActive = true
button.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
现在编写通过约束动画移动视图的函数:
@objc func handletapButton() {
UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseOut, animations: {
self.up?.isActive = false
self.down?.isActive = true
self.view.layoutIfNeeded()
}, completion: nil)
}
这是结果