简体   繁体   English

如何在UIScrollView中加载UIViewController

[英]How to load a UIViewController inside an UIScrollView

This is my setup. 这是我的设置。 I have an UIScrollView on top of my main view controller in which I load multiple view controllers. 我在主视图控制器的顶部有一个UIScrollView ,我在其中加载了多个视图控制器。 I also have an Add button which will present a new view controller using a Push segue. 我还有一个Add按钮,它将使用Push segue呈现一个新的视图控制器。

I want this view controller to also load only on top of the scroll view and not the all screen. 我希望这个视图控制器也只在滚动视图的顶部而不是全屏加载。
I tried 2 different things until now, but none of the work: 到目前为止,我尝试了两种不同的东西,但没有任何工作:

  1. Add view controller inside scroll view in prepareForSegue : prepareForSegue滚动视图中添加视图控制器:

      override func prepare(for segue: UIStoryboardSegue, sender: Any?) { let addViewController = segue.destination as! AddViewController addChildViewController(addViewController) addViewController.view.frame = CGRect(x: 0, y: 0, width: width, height: height) scrollView.addSubview(addViewController.view) didMove(toParentViewController: self) } 
  2. Add view controller in UIButton action: 在UIButton操作中添加视图控制器:

@IBAction func addDidTouch(_ sender: AnyObject) { @IBAction func addDidTouch(_ sender:AnyObject){

    let addViewController = AddViewController()

    addChildViewController(addViewController)
    addViewController.view.frame = CGRect(x: 0, y: 0, width: width, height: height)
    scrollView.addSubview(addViewController.view)
    didMove(toParentViewController: self)
}

Both of this solutions crashes my app. 这两个解决方案都崩溃了我的应用程序。
Is there a way to implement this correctly ? 有没有办法正确实现这个?

You cannot push any view controller on the same view controller, you need to add container view to your scroll view. 您无法在同一视图控制器上推送任何视图控制器,您需要将容器视图添加到滚动视图。 And then if you want you may scroll the scroll on tap of the add button, so that it will seem like new controller is being added to it. 然后,如果你想要你可以滚动滚动按钮添加按钮,这样看起来好像新的控制器被添加到它。 It can be done like this, 它可以这样做,

scrollView.contentSize = CGSize(width: screenWidth*3, height: 1)

    let first = getStoryboard(StoryboardName.Main).instantiateViewControllerWithIdentifier("FirstViewController") as! FirstViewController

    let second = getStoryboard(StoryboardName.Main).instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController

    let third = getStoryboard(StoryboardName.Main).instantiateViewControllerWithIdentifier("ThirdViewController") as! ThirdViewController

    self.addChildViewController(first)
    self.scrollView.addSubview(first.view)
    first.willMoveToParentViewController(self)

    self.addChildViewController(second)
    self.scrollView.addSubview(second.view)
    second.willMoveToParentViewController(self)

    self.addChildViewController(third)
    self.scrollView.addSubview(third.view)
    third.willMoveToParentViewController(self)

    first.view.frame.origin = CGPointZero
    second.view.frame.origin = CGPoint(x: screenWidth, y: 0)
    third.view.frame.origin = CGPoint(x: 2*screenWidth, y: 0)

You may want to disable scroll of your scroll view if you want just to add(move) to another view controller only by your add button. 如果您只想通过添加按钮添加(移动)到另一个视图控制器,则可能需要禁用滚动视图的滚动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM