Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to move between Two UIViews Independent of the ViewController in iOS

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 519
    Comment on it

    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.

     

     

    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.

     

     

    Thanks for reading, Hope it helps. For any question, please feel free tow write in comments.

    How to move between Two UIViews Independent of the ViewController in iOS

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: