Using Swift 3
In this tutorial, we’re going to use a segment control to display view controllers when clicked by the hiding method.
In my project, I had a plain view controller to which I added a UISegmentControl and two UIContainerViews – one layered on top of each other; it is the UIView object you see in the image above. When you add a UIContainerView, it will automatically add a sub view where you will add elements to it. Since I added two, it will display two sub views – which I added a label to each to display which one was which.
I then created @IBOutlet connections for the UISegmentControl and UIContainerViews in the ViewController.swift file. I also created an @IBAction connection for the UISegmentControl which will trigger anytime it’s selected index changes.
So, whenever a user clicks on the first index of the UISegmentControl, it will hide the second container and display the first – and vice-versa for the second index.
It is also important to remember to figure out which UIContainerView should be layered on top, so when the project runs, it originally displays – or you can do this in code as well.