Reputation: 589
I created 2 Button in my superView , Now I want change the @IBOutlet weak var bottomLayOut: NSLayoutConstraint!
depend on the user role .
the example is: if the user is a agent role but not a teacher role I want update the NSLayoutConstraint
's second item from Teacher Entry .bottom to AgentEntry button's bottom .
Update :
solve this by turn translatesAutoresizingMaskIntoConstraints
to true:
e.g : teacherBtn.translatesAutoresizingMaskIntoConstraints = true
than, we can use both constraints and code to change teacherBtn's frame
Upvotes: 0
Views: 686
Reputation: 17050
Two ways I can think of to do this off the top of my head:
` if let constraints = 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`
Upvotes: 2