Setting up UIScrollView to swipe between 3 view controllers

前端 未结 4 1885

I am trying to set up a UIScrollView so that I can swipe between my 3 view controllers. This is my code in AppDelegate.m:

- (BOOL)application:(UIApplication          


        
相关标签:
4条回答
  • 2020-12-03 00:26

    UPD: June, 2015 Swift

    The concept remains the same, which is described below in Objective-C section. There is a little change in syntax. To add childviewcontroller use following snippet:

    let aViewController = storyboard.instantiateViewControllerWithIdentifier("A") as! AViewController;
    
    addChildViewController(aViewController);
    scrollView!.addSubview(aViewController.view)
    aViewController.didMoveToParentViewController(self)
    

    Check my Swift Github Sample Code

    Objective-C

    Create your own custom container view controller (I will call it combinedViewController), which will hold your three controllers in scroll view. Inherit like you always do UIViewController, then use addChildViewController public API in your new combinedViewController -viewDidLoad: like this:

    [self addChildViewController:aViewController];
    [self.scrollView addSubview:aViewController.view];
    [aViewController didMoveToParentViewController:self];
    

    Here’s what the code does:

    • It calls the container’s addChildViewController: method to add the child.
    • It accesses the child’s view property to retrieve the view and adds it to its own view hierarchy. The container sets the child’s size and position before adding the view; containers always choose where the child’s content appears.
    • It explicitly calls the child’s didMoveToParentViewController: method to signal that the operation is complete.

    Do this operation with each of your viewControllers. Afterwards, set your combinedViewController as a rootViewController.

    if you need further explanation, feel free to ask.

    Reference: Design custom container view controller

    Here you are my Objective-C Github sample code

    UPD: Thanks @Oliver Atkinson for clarifying that addChildViewController: method also calls the child’s willMoveToParentViewController: method automatically.

    Results:

    0 讨论(0)
  • 2020-12-03 00:33
        let obj1 = self.storyboard?.instantiateViewControllerWithIdentifier("DocumentsVC") as! DocumentsVC
        let obj2 = self.storyboard?.instantiateViewControllerWithIdentifier("AppointmentsVC") as! AppointmentsVC
        let obj3 = self.storyboard?.instantiateViewControllerWithIdentifier("DashboardVC") as! DashboardVC
    
        self.containerScrollView.frame = obj2.view.frame
    
        self.containerScrollView.addSubview(obj2.view)
        obj2.willMoveToParentViewController(self)
        self.addChildViewController(obj2)
    
        self.containerScrollView.addSubview(obj1.view)
        obj1.willMoveToParentViewController(self)
        self.addChildViewController(obj1)
    
        self.containerScrollView.addSubview(obj3.view)
        obj3.willMoveToParentViewController(self)
        self.addChildViewController(obj3)
    
        self.containerScrollView.contentSize = CGSizeMake(3*UIScreen.mainScreen().bounds.width, 0)
    
        obj1.view.frame.origin =  CGPointMake(0, 0)
        obj2.view.frame.origin =  CGPointMake(UIScreen.mainScreen().bounds.width, 0)
        obj3.view.frame.origin = CGPointMake(2*UIScreen.mainScreen().bounds.width, 0)
    
    0 讨论(0)
  • 2020-12-03 00:34

    Swift 3.0

    Based on Sachin answer - bit more generic - just add next element to views array

    var views = [ViewController(), ViewController2(), ViewController3(), ViewController4()]
    
    func setupScrollView() {
        scrollView.frame = views.first!.view.frame
        scrollView.contentSize = CGSize(width: CGFloat(views.count) * width, height: 0)
        _ = views.map({ addViewToScrollView($0) })
        _ = views.map({ $0.view.frame.origin =  CGPoint(x: CGFloat(views.index(of: $0)!) * width, y: 0) })
    }
    
    func addViewToScrollView(_ viewController: UIViewController) {
        scrollView.addSubview(viewController.view)
        views.willMove(toParentViewController: self)
        addChildViewController(viewController)
    }
    
    0 讨论(0)
  • 2020-12-03 00:51

    Try this git repo, Using this repo you can create a view navigation like Snapchat/Tinder Main Pages.

    https://github.com/goktugyil/EZSwipeController

    0 讨论(0)
提交回复
热议问题