Vandan Patel
Vandan Patel

Reputation: 1022

Multiple Variations of UI for the same view controller

I am working on an Exercise App, where UI depends on the type of Exercise. Some exercises have the question as text and answer as text. Some exercises have the question as text and answer as images. One more variation is image and text in question and image in the answer. I create a question object based on the values I get from API for a particular Exercise. That object has many fields as optional. For example, the image is optional. Now the challenge I am facing is what is the best way to handle such dynamic UI. In plain English, if it has the image, show the imageView and if it doesn't have the image, don't show the imageView and adjust other UI elements accordingly.

Upvotes: 0

Views: 227

Answers (1)

Nicolas Miari
Nicolas Miari

Reputation: 16246

You can have one UIViewController subclass, and many different storyboards each with a different layout and subviews.

  • Each storyboard has the view controller's class set to your custom class in the identity inspector.

  • Your view controller has outlets for all possible subviews, and each storyboard connects its subviews to the outlets that are relevant to it.

  • When you need to present a particular variation of the string, you instantiate your view controller from a case-specific storyboard (programmatically or using segues and storyboard references).


If you only need to disable one particular subview in one case, you can do one of the following:

  • Set that subview's isHidden property to false. It will stay in place, and occupy the same area, but invisible.
  • Remove it from the main view by calling removeFromSuperview(). Be careful if other, remaining subviews rely on constraints against the removed subview for their layout, though.

Upvotes: 1

Related Questions