如何使用Swift在iOS 8中正确添加子视图控制器

时间:2022-03-21 20:53:50

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

我一直在阅读文档,似乎仍然是一个棘手的问题。

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.

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

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.

为了弄清楚发生了什么,我用颜色编码了一切。下面的容器是橙色的。容器所期望的视图,根据故事板是黄色的。我实际上想要适应的视图是红色的。

Here is the storyboard:

这是故事板:

如何使用Swift在iOS 8中正确添加子视图控制器

Here is my controller code for C_SelectPhoto

这是我的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: 如何使用Swift在iOS 8中正确添加子视图控制器

但结果是产生如下:

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.

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

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?

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

Thank you!!!

谢谢!!!

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.

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

2 个解决方案

#1


4  

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.

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

#2


10  

use following Extension for adding childViewController On View

使用以下扩展名在View上添加childViewController

extension UIViewController {   
func configureChildViewController(childController: UIViewController, onView: UIView?) {
    var holderView = self.view
    if let onView = onView {
        holderView = onView
    }
    addChildViewController(childController)
    holderView.addSubview(childController.view)
    constrainViewEqual(holderView, view: childController.view)
    childController.didMoveToParentViewController(self)
    childController.willMoveToParentViewController(self)
}


func constrainViewEqual(holderView: UIView, view: UIView) {
    view.translatesAutoresizingMaskIntoConstraints = false
    //pin 100 points from the top of the super
    let pinTop = NSLayoutConstraint(item: view, attribute: .Top, relatedBy: .Equal,
        toItem: holderView, attribute: .Top, multiplier: 1.0, constant: 0)
    let pinBottom = NSLayoutConstraint(item: view, attribute: .Bottom, relatedBy: .Equal,
        toItem: holderView, attribute: .Bottom, multiplier: 1.0, constant: 0)
    let pinLeft = NSLayoutConstraint(item: view, attribute: .Left, relatedBy: .Equal,
        toItem: holderView, attribute: .Left, multiplier: 1.0, constant: 0)
    let pinRight = NSLayoutConstraint(item: view, attribute: .Right, relatedBy: .Equal,
        toItem: holderView, attribute: .Right, multiplier: 1.0, constant: 0)

    holderView.addConstraints([pinTop, pinBottom, pinLeft, pinRight])
}}

#1


4  

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.

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

#2


10  

use following Extension for adding childViewController On View

使用以下扩展名在View上添加childViewController

extension UIViewController {   
func configureChildViewController(childController: UIViewController, onView: UIView?) {
    var holderView = self.view
    if let onView = onView {
        holderView = onView
    }
    addChildViewController(childController)
    holderView.addSubview(childController.view)
    constrainViewEqual(holderView, view: childController.view)
    childController.didMoveToParentViewController(self)
    childController.willMoveToParentViewController(self)
}


func constrainViewEqual(holderView: UIView, view: UIView) {
    view.translatesAutoresizingMaskIntoConstraints = false
    //pin 100 points from the top of the super
    let pinTop = NSLayoutConstraint(item: view, attribute: .Top, relatedBy: .Equal,
        toItem: holderView, attribute: .Top, multiplier: 1.0, constant: 0)
    let pinBottom = NSLayoutConstraint(item: view, attribute: .Bottom, relatedBy: .Equal,
        toItem: holderView, attribute: .Bottom, multiplier: 1.0, constant: 0)
    let pinLeft = NSLayoutConstraint(item: view, attribute: .Left, relatedBy: .Equal,
        toItem: holderView, attribute: .Left, multiplier: 1.0, constant: 0)
    let pinRight = NSLayoutConstraint(item: view, attribute: .Right, relatedBy: .Equal,
        toItem: holderView, attribute: .Right, multiplier: 1.0, constant: 0)

    holderView.addConstraints([pinTop, pinBottom, pinLeft, pinRight])
}}