user3926776
user3926776

Reputation:

scrollView swipe between views

I'm trying to create a navigation like snapChat where you swipe between different views. i've created this by making a containViewController with a scrollView and then have 3 viewControllers which will be presented in the scrollView. This works fine, but it will always present the first viewController. I want it to present the second viewController first so you can scroll right and left to the other viewControllers. How can i obtain this.

containViewController

override func viewDidLoad() {
    super.viewDidLoad();
    
    // 1) Create the three views used in the swipe container view
    var BVc :BViewController =  BViewController(nibName: "BViewController", bundle: nil);
    var AVc :AViewController =  AViewController(nibName: "AViewController", bundle: nil);

    var CVc :CViewController =  CViewController(nibName: "CViewController", bundle: nil);
    
    
    // 2) Add in each view to the container view hierarchy
    //    Add them in opposite order since the view hieracrhy is a stack


    self.addChildViewController(BVc);
    self.scrollView!.addSubview(BVc.view);
    BVc.didMoveToParentViewController(self);
    
    self.addChildViewController(AVc);
    self.scrollView!.addSubview(AVc.view);
    AVc.didMoveToParentViewController(self);
    
    self.addChildViewController(CVc);
    self.scrollView!.addSubview(CVc.view);
    CVc.didMoveToParentViewController(self);
    
    
    // 3) Set up the frames of the view controllers to align
    //    with eachother inside the container view
    
    var BFrame :CGRect = BVc.view.frame;
    BFrame.origin.x = 2*BFrame.width;
    CVc.view.frame = BFrame;
    
    var adminFrame :CGRect = AVc.view.frame;
    adminFrame.origin.x = adminFrame.width;
    BVc.view.frame = adminFrame;
    

    

    // 4) Finally set the size of the scroll view that contains the frames
    var scrollWidth: CGFloat  = 3 * self.view.frame.width
    var scrollHeight: CGFloat  = self.view.frame.size.height
    self.scrollView!.contentSize = CGSizeMake(scrollWidth, scrollHeight);
}

Upvotes: 0

Views: 199

Answers (2)

Rob
Rob

Reputation: 1034

If I understand correctly you have 3 ContainerVCs inside a scrollView?

If so, just change the scrollView's contentOffset value to wherever your second VC is. I'm not familiar with swift but in Obj-C you'd do something like this:

self.scrollView!.contentOffset = CGPointMake(BVc.view.frame.origin.x, 0);

Upvotes: 0

Dev138
Dev138

Reputation: 336

I would Suggest using UIPageViewController, implement methods from UIPageViewControllerDataSource.

UIPageViewController allows you to swipe between ViewControllers giving you the power to configure the order of the ViewControllers and the starting index.

Hope this helps!

Upvotes: 1

Related Questions