0

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 .

enter image description here is that posible?

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

Jared Updike
  • 7,165
  • 8
  • 46
  • 72
Neko
  • 581
  • 1
  • 9
  • 24
  • just wondering, why not you change the button name and color based on the role instead of changing constraints? – Julfikar Aug 29 '17 at 02:35
  • the 2 button can be show on same time – Neko Aug 29 '17 at 02:39
  • let me understand a bit clearly.. let's say the user role is teacher, so you want to show the teacher button on top and agent button on bottom, isn't it? – Julfikar Aug 29 '17 at 02:45
  • there are 3 patterns : both teacher and agent role - show both 2 button, only teacher role - show teacher button , only agent role - show agent button. – Neko Aug 29 '17 at 02:48
  • ok not sure how you wanna do it but i can suggest using `setFrame:CGRectMake` . this one you can use for positioning the buttons and to hide buttons you can use `button.hidden = YES;` . I would recommend you not to mess up with constraint – Julfikar Aug 29 '17 at 04:31
  • but once you used Autolayout, you can't use code to change the frame , right? – Neko Aug 29 '17 at 05:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/153081/discussion-between-neko-and-mohammad-julfikar). – Neko Aug 29 '17 at 05:37
  • Use autolayout programatically. case 1- If both teacher and agent role - dont hidden any button. case 2 - only teacher role - agent button make it hidden and make bottom constraint of teacher button isactive false. And give top constraint for that button(programatically). 3 - only agent role - Just hidden the teacher button. – Pavankumar Aug 29 '17 at 12:48

1 Answers1

2

Two ways I can think of to do this off the top of my head:

  1. Set up the constraints programmatically. Create a property containing the current constraints on the buttons, and then you can do something along the lines of (disclaimer: written in Chrome, may contain typos, edit as appropriate):

` 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`
  1. What might be simpler, though, is to use a UIStackView to hold your buttons. On current macOS at least, NSStackView automatically adjusts the layout based on which of its views are hidden; if UIStackView on iOS behaves the same way, it may do a lot of the work for you without having to manually fuss with layout constraints.
Charles Srstka
  • 16,665
  • 3
  • 34
  • 60