如何在UIScrollView中加载UIViewController [英] How to load a UIViewController inside an UIScrollView

查看:282
本文介绍了如何在UIScrollView中加载UIViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的设置。我在我的主视图控制器上有一个 UIScrollView ,我在其中加载了多个视图控制器。我还有一个Add按钮,它将使用Push segue呈现一个新的视图控制器。

This is my setup. I have an UIScrollView on top of my main view controller in which I load multiple view controllers. I also have an Add button which will present a new view controller using a 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. 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)
}


  • 在UIButton操作中添加视图控制器:

  • Add view controller in UIButton action:

    @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.

    这篇关于如何在UIScrollView中加载UIViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆