bmt22033
bmt22033

Reputation: 7250

Swapping UIViews in and out of a ViewController's view

I'm not entirely new to iOS/Swift development but I've mostly been working on "backend" code so my UI design/development skill level is unfortunately not what I'd like it to be. With that in mind, I have a UI layout (for a view controller) that looks something like this:

enter image description here

In this layout, the view labeled "View To Swap" could be in one of three states:

  1. It may be hidden (or removed?) in which case StackView B should appear directly below StackView A
  2. It may need to show (or be replaced by?) "Subview A"
  3. It may need to show (or be replaced by?) "Subview B"

Both Subview A and B will have a button in the upper right hand corner that, when clicked, should remove/hide the "View To Swap" view.

I've been reading about container views a little bit and I'm wondering if this is the approach that I should take here or is there a more simple, direct way to implement this? I'm currently using the storyboard and autolayout to define my primary view (the view on the left) and constraints but I understand the basics of building a UI programatically if that makes it easier to implement something like this? Thanks for any suggestions!

Upvotes: 2

Views: 702

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100541

You need to insert the 3 main views ( top stack , viewToSwap , bottom stack ) inside a vertical UIStackView with distribution = fillEqually , and hook viewToSwap as IBOutlet, whenever you want to hide it do

 self.viewToSwap.isHidden = true

this will automatically collapse it's content , and you can add any subview to it (A/B) and don't forget self.viewToSwap.clipsToBounds = true

Upvotes: 4

Related Questions