使用函数停用 viewdidload 中声明的 nslayoutconstraint
deactivate nslayoutconstraint declared in viewdidload with function
我下面的代码在 viewdidload 中声明了布局约束。当调用函数 Selena 时,我希望将约束更改为函数 selena 中的内容。当我调用函数时,我不会更改对象。它没有效果。我知道我必须以某种方式停用原始约束。
override func viewDidLoad() {
NSLayoutConstraint.activate ([
i.trailingAnchor.constraint(equalTo: view.centerXAnchor, constant :150),
i.topAnchor.constraint(equalTo: view.centerYAnchor, constant : -200),
i.widthAnchor.constraint(equalToConstant: 300),
i.heightAnchor.constraint(equalToConstant: 450),
])
}
@objc func selena(){
NSLayoutConstraint.activate ([
i.trailingAnchor.constraint(equalTo: view.centerXAnchor, constant :50),
i.topAnchor.constraint(equalTo: view.centerYAnchor, constant : -50),
i.widthAnchor.constraint(equalToConstant: 20),
i.heightAnchor.constraint(equalToConstant: 450),
])
}
您需要将约束分配给变量,并在需要更改它们时更改其常量。这是带有尾随约束的示例。无需更改锚点,只需更改常量即可。这就是你的做法。
var iTrailingConstraint:NSLayoutConstraint?
override func viewDidLoad() {
iTrailingConstraint = i.trailingAnchor.constraint(equalTo: view.centerXAnchor, constant :150).isActive = true
}
调用 selena 时,您将常量设置为所需的动画。在控制器视图上调用 layoutIfNeeded()
@objc func selena(){
UIView.animate(withDuration: 0.5, animations: {
self.iTrailingConstraint?.constant = 50
self.view.layoutIfNeeded()
})
}
您可以引用它们然后使用 deactivate
var oldCons = [NSLayoutConstraint]()
override func viewDidLoad() {
super.viewDidLoad()
oldCons = [
i.trailingAnchor.constraint(equalTo: view.centerXAnchor, constant :150),
i.topAnchor.constraint(equalTo: view.centerYAnchor, constant : -200),
i.widthAnchor.constraint(equalToConstant: 300),
i.heightAnchor.constraint(equalToConstant: 450)
]
NSLayoutConstraint.activate(oldCons)
}
@objc func selena(){
NSLayoutConstraint.deactivate(oldCons)
NSLayoutConstraint.activate ([
i.trailingAnchor.constraint(equalTo: view.centerXAnchor, constant :50),
i.topAnchor.constraint(equalTo: view.centerYAnchor, constant : -50),
i.widthAnchor.constraint(equalToConstant: 20),
i.heightAnchor.constraint(equalToConstant: 450)
])
}
height connstraint没有改变你可以从viewDidLoad
里面的数组中取出来
i.heightAnchor.constraint(equalToConstant: 450).isActive = true
并将其从 selena
函数中删除
我下面的代码在 viewdidload 中声明了布局约束。当调用函数 Selena 时,我希望将约束更改为函数 selena 中的内容。当我调用函数时,我不会更改对象。它没有效果。我知道我必须以某种方式停用原始约束。
override func viewDidLoad() {
NSLayoutConstraint.activate ([
i.trailingAnchor.constraint(equalTo: view.centerXAnchor, constant :150),
i.topAnchor.constraint(equalTo: view.centerYAnchor, constant : -200),
i.widthAnchor.constraint(equalToConstant: 300),
i.heightAnchor.constraint(equalToConstant: 450),
])
}
@objc func selena(){
NSLayoutConstraint.activate ([
i.trailingAnchor.constraint(equalTo: view.centerXAnchor, constant :50),
i.topAnchor.constraint(equalTo: view.centerYAnchor, constant : -50),
i.widthAnchor.constraint(equalToConstant: 20),
i.heightAnchor.constraint(equalToConstant: 450),
])
}
您需要将约束分配给变量,并在需要更改它们时更改其常量。这是带有尾随约束的示例。无需更改锚点,只需更改常量即可。这就是你的做法。
var iTrailingConstraint:NSLayoutConstraint?
override func viewDidLoad() {
iTrailingConstraint = i.trailingAnchor.constraint(equalTo: view.centerXAnchor, constant :150).isActive = true
}
调用 selena 时,您将常量设置为所需的动画。在控制器视图上调用 layoutIfNeeded()
@objc func selena(){
UIView.animate(withDuration: 0.5, animations: {
self.iTrailingConstraint?.constant = 50
self.view.layoutIfNeeded()
})
}
您可以引用它们然后使用 deactivate
var oldCons = [NSLayoutConstraint]()
override func viewDidLoad() {
super.viewDidLoad()
oldCons = [
i.trailingAnchor.constraint(equalTo: view.centerXAnchor, constant :150),
i.topAnchor.constraint(equalTo: view.centerYAnchor, constant : -200),
i.widthAnchor.constraint(equalToConstant: 300),
i.heightAnchor.constraint(equalToConstant: 450)
]
NSLayoutConstraint.activate(oldCons)
}
@objc func selena(){
NSLayoutConstraint.deactivate(oldCons)
NSLayoutConstraint.activate ([
i.trailingAnchor.constraint(equalTo: view.centerXAnchor, constant :50),
i.topAnchor.constraint(equalTo: view.centerYAnchor, constant : -50),
i.widthAnchor.constraint(equalToConstant: 20),
i.heightAnchor.constraint(equalToConstant: 450)
])
}
height connstraint没有改变你可以从viewDidLoad
i.heightAnchor.constraint(equalToConstant: 450).isActive = true
并将其从 selena
函数中删除