Hey Everyone,
Is it possible to dynamically change a UI element on a storyboard based on a specific variable value? For instance, if a boolean variable is set to true, display a UISlider. If the variable is false, display 2 buttons in the exact same place?
Is this possible or should I set up two different scenes to have 2 different looks?
Thank you,
Jason
Sure it is - anything you can do in the Storyboard you can do in code. One of the most common things you will do is set the isHidden member to true or false, hiding or showing anything descending from a UIView. Another common one is to set the value of a constraint’s constant.
For your example you would put both the buttons and the slider in the same place, then choose which set of elements to display. It would be best to define a function that chooses either the buttons (as a pair) or the slider, since it would look bad if you ever forgot to hide a button while showing the slider…
The point where you decide to have a whole different view controller is really up to you, but if things start getting hard to manage with all your adjustments and control movements, that’s where the boundary should be.
Perfect, thanks aeberbach! Sometimes problems seem harder than they are
Hey it just occurred to me I didn’t mention @IBOutlet! You need to make a link between your storyboard item and the code that will reference it. The Assistant Editor (looks like two overlapping circles) will bring up the code that corresponds to any selected view controller - if it doesn’t, change “Manual” to “Automatic” - then control-drag from the object to the code and select outlet. Whatever name you give it in the outlet is the reference you use when accessing properties like “isHidden”. Good luck!
Thanks aeberbach! Much appreciated!