[英]How to Update NSLayoutConstraint in StoryBoard?
我在 superView 中創建了 2 個 Button,現在我想更改@IBOutlet weak var bottomLayOut: NSLayoutConstraint!
取決於用戶角色。
示例是:如果用戶是代理角色但不是教師角色,我想將NSLayoutConstraint
的第二項從 Teacher Entry .bottom 更新為 AgentEntry 按鈕的 bottom 。
更新 :
通過將translatesAutoresizingMaskIntoConstraints
為 true 來解決這個問題:例如: teacherBtn.translatesAutoresizingMaskIntoConstraints = true
比,我們可以同時使用約束和代碼來更改 teacherBtn 的框架
我可以想到兩種方法來做到這一點:
` 如果讓約束 = self.buttonConstraints { NSLayoutConstraint.deactivate(constraints) }
let views: [String : UIView] = ["Agent" : self.agentButton, "Teacher" : self.teacherButton]
let newConstraints: [NSLayoutConstraint] = {
switch role {
case .teacherAndAgent:
self.teacherButton.isHidden = false
self.agentButton.isHidden = false
return NSLayoutConstraint.constraints(withVisualFormat: "V:[agent]-[teacher]-|", metrics: nil, views: views)
case .teacherOnly:
// you get the idea
case .agentOnly:
// ditto
}
}()
NSLayoutConstraint.activate(newConstraints)
self.buttonConstraints = newConstraints`
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.