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。向视图控制器添加一个委托,其视图将会更改,然后让您的父级视图控制器将其自己作为委托。然后,您可以检查意见并相应调整大小。