0

我目前有一个 UI 类似于 Snapchat ScrollView 的应用程序。MainViewController 包含一个 UIScroll,其中添加了三个子 ViewController 作为子 ViewController:

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 中添加两个按钮,这会将 ScrollView 偏移到左侧或右侧的 View Controller 上。我知道我可以在 MainViewController 中使用 setContentOffset(),但我不能在子视图控制器中调用它。self.presentingViewController 和 self.parentViewController 都返回 nil。

在子 ViewController 中为 UIScrollView 调用 setContentOffset() 的最佳方法是什么?

4

1 回答 1

0

这似乎是委托的一个很好的用例。向 ViewControllers 添加一个委托,其视图将发生变化,然后让您的父 ViewController 将自己作为委托。然后您可以检查视图并相应地调整大小。

于 2016-03-21T22:39:38.133 回答