如何使用Swift在iOS 8中正确添加子视图控制器 [英] How To Properly Add Child View Controller in iOS 8 With Swift

查看:254
本文介绍了如何使用Swift在iOS 8中正确添加子视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读文档,但似乎仍然处于困境。

I've been going through the documentation and still seem to be on a sticking point.

我有一个视图控制器对象C_SelectPhoto。这有一个容器视图。在容器视图中,我希望childed视图控制器C_SelectPhotoControllerView适合它。它只是一系列照片。但是,设置框架和添加子视图控制器不起作用。如果我移动所需子视图控制器的x值,则不会发生任何影响。

I have a view controller object C_SelectPhoto. This has a container view. Inside the container view I want the childed view controller, C_SelectPhotoControllerView, to fit inside it. It will just be an array of photos. However, setting the frame and adding the child view controller is not working. If I move the x value of the desired child view controller, no effect happens.

要弄清楚发生了什么,我会对所有内容进行颜色编码。下面的容器是橙色的。容器所期望的视图,根据故事板是黄色的。我实际上想要的视图是红色。

To figure out what is going on I color coded everything. The container, below, is orange. The view the container expects, according to the storyboard is yellow. The view I actually want to fit in there is red.

这是故事板:

这是我的C_SelectPhoto控制器代码

Here is my controller code for C_SelectPhoto

class C_SelectPhoto:Controller
{
    @IBOutlet weak var selectPhotoControllerView: UIView!
    var _collectionViewController:C_SelectPhotoControllerView!

    //TODO PERMISSION IS NEEDED BEFORE FETCHING
    func initController()
    {   
        _collectionViewController = Controller.STORYBOARD.instantiateViewControllerWithIdentifier("selectPhotoControllerView") as C_SelectPhotoControllerView
        displayControllerViewController()
    }

    //show the photo selection
    private func displayControllerViewController()
    {
        addChildViewController(_collectionViewController)
        _collectionViewController.view.frame = CGRectMake(100, 0, 500, 500)
        self.view.addSubview(_collectionViewController.view)
        _collectionViewController.didMoveToParentViewController(self)
    }
}

但产生的结果如下:

However the result is produces is below:

首先,根本不应添加黄色类,我想要只有红色(UI CollectionViewController类)。其次,我可以告诉红色类被添加到错误的位置,因为它的x值根本没有移动它。

First, the yellow class shouldn't be added at all, I wanted only the red (the UICollectionViewController class). Second, I can tell the red class is being added to the wrong spot because its x value hasn't moved it over at all.

所以我的问题是:
如何将UIContainerViewController作为子视图添加到主视图控制器C_SelectPhoto中,但是具有UIContainerViewController框架FIT我在主视图控制器中拥有的容器?

So my question is: How can I add a UIContainerViewController, as a child to the main view controller, C_SelectPhoto, but have the UIContainerViewController frame FIT the container I have in the main view controller?

谢谢!

注意:我想添加的视图是UICollectionViewControllers。当我添加一个UIViewController时,框架工作正常,但正如您在添加UICollectionViewControllers时所看到的那样,框架不起作用,它们会被添加到随机偏移中,并且不尊重我尝试使用框架分配来调整它们的大小。 / p>

NOTE: The views I am trying to add are UICollectionViewControllers. When I add a UIViewController, the framing works just fine, but as you can see when adding the UICollectionViewControllers, the framing does NOT work, and they are getting added to random offsets and are not respecting my attempts to size them with frame assignments.

推荐答案

如果您希望红色控制器成为子控制器,请删除黄色控制器,然后从容器中控制拖动到红色控制器。无需在代码中添加它,也不需要进行任何大小调整。红色控制器将设置为与故事板中容器相同的大小。

If you want the red controller to be the child controller, delete the yellow one, and control-drag from the container to the red controller. There's no need to add it in code, or do any resizing. The red controller will be set to the same size as the container in the storyboard.

这篇关于如何使用Swift在iOS 8中正确添加子视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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