【问题标题】:Offset UIScrollView from a child ViewController从子 ViewController 偏移 UIScrollView
【发布时间】:2016-07-08 14:34:05
【问题描述】:

我目前有一个 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() 的最佳方法是什么?

【问题讨论】:

    标签: ios swift uiviewcontroller uiscrollview parentviewcontroller


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-05
      • 2016-06-08
      • 2015-10-09
      • 2011-06-07
      • 2013-09-28
      • 2012-09-29
      • 2014-07-03
      • 1970-01-01
      相关资源
      最近更新 更多