Before moving on to the topic, first of all, I assume you all are aware of Storyboards, segues plus traveling from one view controller to another. So going from one VC to another is okay what if we need to move between two UiView’s independent of UIViewController.
![](http://findnerd.s3.amazonaws.com/imagedata/9711/9711.png)
So to perform this, what we can do is grab two views & one button outside of these two views. We can initially display View1 & show whatever desired on View1 keeping View2 hidden at that time. Whereas when we tap on the Button/Segmented Control we can hide the currently visible view & display View2 with its contents.
Another approach is, take a UISegmented control & set its tap action according to the view you want to show or hide.
Here is the code snippet I have used to perform the task.
Here we have declared both the views along with an UISegmented Control
@IBOutlet weak var view1: UIView!
@IBOutlet weak var view2: UIView!
@IBOutlet weak var segmentedControl: UISegmentedControl!
Now all we need to do is set action of UISegmented control as shown
@IBAction func segmentClicked(_ sender: Any) {
switch segmentedControl.selectedSegmentIndex
{
case 0:
NSLog("View1 selected")
view2.isHidden = true
view1.isHidden = false
//show popular view
case 1:
NSLog("View2 selected")
view1.isHidden = true
view2.isHidden = false
//show history view
default:
break;
}
}
Take a look at attached screenshots & design your storyboard accordingly.
![](http://findnerd.s3.amazonaws.com/imagedata/9705/9705.png)
Thanks for reading, Hope it helps. For any question, please feel free tow write in comments.
0 Comment(s)