Leem
Leem

Reputation: 18308

Define a storyboard that could switch between views at runtime

I would like to implement a simple UI which has one switch button, when switch is turned on the app shows one view, when turned off shows another view. (There is also an main button at the bottom which should always be visible no matter which UIView is shown)

I am thinking on storyboard, I use one UIVIewController, then define two UIView components each represent one view.

enter image description here

So, in short, I would like to show different UIViews at runtime between the switch button and the Main button.

But I get stuck, I am not sure how to show one of the UIView by default with my planned solution above. (Probably I am not sure whether it is correct way to do what I want to achieve.) Could someone please guide me a bit?

Upvotes: 1

Views: 364

Answers (2)

Andrea
Andrea

Reputation: 26385

You can use 2 different approaches:

  1. Swap views: this can be done easily by using UIView animation, for instance this method transition(from:to:duration:options:completion:)
  2. Use contentment view controller API as already said by Wesley
    While the first one will do the trick, if your views have pretty different behaviors and business logic is better to use 2 different view controllers and use a container view. For more info click here and here.

Upvotes: 0

Wesley
Wesley

Reputation: 2200

Within a storyboard you could add two container views. These are basically UIViewController added as children. Based on the switch, you could hide/show with the isHidden parameter one or the other.

You should use this element twice

Upvotes: 1

Related Questions