0

I created a scroll view container that houses three view controllers. It's meant to mimic snapchat's swipe layout. however, I can't seem to get a code to manually switch to the next view controller without actually swiping (which I'm not interested in)

I tried calling the container class and setting it's scroll offset but it crashes... tried creating a delegate protocol, but delegate is returning nil... I'm stumped.

Here is my code:

class AViewController: UIViewController, ABViewControllerDelegate {

    @IBOutlet var scrollView: UIScrollView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // 1) Create the three views used in the swipe container view
        var ATVc : ATViewController =  ATViewController(nibName: "ATViewController", bundle: nil);
        var ACVc : ACViewController =  ACViewController(nibName: "ACViewController", bundle: nil);
        var ABVc : ABViewController =  ABViewController(nibName: "ABViewController", 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(ABVc);
        self.scrollView!.addSubview(ABVc.view);
        ABVc.didMoveToParentViewController(self);

        self.addChildViewController(ACVc);
        self.scrollView!.addSubview(ACVc.view);
        ACVc.didMoveToParentViewController(self);

        self.addChildViewController(ATVc);
        self.scrollView!.addSubview(ATVc.view);
        ATVc.didMoveToParentViewController(self)

        // 3) Set up the frames of the view controllers to align
        //    with eachother inside the container view
        var adminFrame :CGRect = ATVc.view.frame;
        adminFrame.origin.y = adminFrame.height;
        ACVc.view.frame = adminFrame;

        var BFrame :CGRect = ACVc.view.frame;
        BFrame.origin.y = 2*BFrame.height;
        ABVc.view.frame = BFrame;

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

        var changeMe : String = "okay"
    }

    func scrollUp() {
        println("clicked!")
        self.scrollView.contentOffset.y - self.view.frame.height
    }
}

and this is the view controller I'm trying to get out off by pressing a button..

protocol ABViewControllerDelegate {
    func scrollUp()
}

class ABViewController: UIViewController {

    let delegate = ABViewControllerDelegate?()

    @IBAction func button(sender: AnyObject) {
        println("button clicked!")
        delegate!.scrollUp()
    }
}

I feel like I'm leading myself on and that it can't be done!

Cœur
  • 37,241
  • 25
  • 195
  • 267
Andrew Aquino
  • 42
  • 1
  • 8
  • I don't think that is a good way to achieve what you're trying. Try using 'UIPageViewController' and set the navigation orientation to 'Vertical' (`UIPageViewControllerNavigationOrientation.Vertical`) Doc : https://developer.apple.com/LIBRARY/ios/documentation/UIKit/Reference/UIPageViewControllerClassReferenceClassRef/index.html#//apple_ref/c/tdef/UIPageViewControllerNavigationOrientation – Pintouch Nov 10 '14 at 09:09
  • 1
    I was able to solve it by using class references since I later learned that its not a parent-child relationship but a view-superview one. So I was able to reference classes till I reached the superview and I was able to access methods and variables. From there I was able to access the scroll variable and do a setOffset.y Thanks for taking the time to answer my question. – Andrew Aquino Nov 10 '14 at 20:14

0 Answers0