0

我目前有一個UI的應用程序類似於Snapchat ScrollView。該MainViewController包含UIScroll,其中三個子ViewControllers添加爲孩子ViewControllers:偏移子ViewController的UIScrollView

override func viewDidLoad() { 

    super.viewDidLoad() 
    scrollView.contentSize = CGSizeMake((self.view.bounds.width*3 + 4), self.view.bounds.height) 
    self.view.addSubview(scrollView) 

    let view1 = UIView(frame: CGRectMake(0, 0, self.view.bounds.width, self.view.bounds.height)) 
    scrollView.addSubview(view1) 
    let aVC = self.storyboard?.instantiateViewControllerWithIdentifier("One") 
    view1.addSubview(aVC!.view) 
    self.addChildViewController(aVC!) 

    let view2 = UIView(frame: CGRectMake((self.view.bounds.width), 0, self.view.bounds.width, self.view.bounds.height)) 
    scrollView.addSubview(view2) 
    let bVC = self.storyboard?.instantiateViewControllerWithIdentifier("Two") 
    view2.addSubview(bVC!.view) 
    self.addChildViewController(bVC!) 

    let view3 = UIView(frame: CGRectMake(2*view.bounds.width), 0, self.view.bounds.width, self.view.bounds.height)) 
    scrollView.addSubview(view3) 
    let cVC = self.storyboard?.instantiateViewControllerWithIdentifier("LocationViewController") 
    view3.addSubview(cVC!.view) 
    self.addChildViewController(cVC!) 

    scrollView.contentOffset = CGPointMake(self.view.frame.width, 0 
    } 

我試圖添加按鈕,兩個中間的ViewController,這將抵消了滾動要麼向左或右視圖控制器。我知道我可以在MainViewController中使用setContentOffset(),但是我不能在子視圖控制器中調用它。 self.presentingViewController和self.parentViewController都返回nil。

什麼是調用setContentOffset()的子ViewController UIScrollView的最佳方式是什麼?

回答

0

這似乎是一個很好的用例delegation。向視圖控制器添加一個委託,其視圖將會更改,然後讓您的父級視圖控制器將其自己作爲委託。然後,您可以檢查意見並相應調整大小。