Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 在循环中添加到ContainerViews_Swift - Fatal编程技术网

Swift 在循环中添加到ContainerViews

Swift 在循环中添加到ContainerViews,swift,Swift,我想使用containerviews包含collectionview的三个实例 我的销售渠道是: @IBOutlet weak var topContainer: UIView! @IBOutlet weak var middleContainer: UIView! @IBOutlet weak var bottomContainer: UIView! 我可以做到:使用一个恶心的解决方案,在viewdidload中重复代码: topContainer.translatesAutores

我想使用containerviews包含collectionview的三个实例

我的销售渠道是:

@IBOutlet weak var topContainer: UIView!
@IBOutlet weak var middleContainer: UIView!
@IBOutlet weak var bottomContainer: UIView!
我可以做到:使用一个恶心的解决方案,在viewdidload中重复代码:

    topContainer.translatesAutoresizingMaskIntoConstraints = false
    middleContainer.translatesAutoresizingMaskIntoConstraints = false
    bottomContainer.translatesAutoresizingMaskIntoConstraints = false
    // add child view controller view to container
    if let controller = storyboard!.instantiateViewController(withIdentifier: "collectionscroll") as? CollectionScrollViewController {
        addChild(controller)
        controller.view.translatesAutoresizingMaskIntoConstraints = false
        topContainer.addSubview(controller.view)

        NSLayoutConstraint.activate([
            controller.view.leadingAnchor.constraint(equalTo: topContainer.leadingAnchor),
            controller.view.trailingAnchor.constraint(equalTo: topContainer.trailingAnchor),
            controller.view.topAnchor.constraint(equalTo: topContainer.topAnchor),
            controller.view.bottomAnchor.constraint(equalTo: topContainer.bottomAnchor)
            ])
        controller.didMove(toParent: self)
    }

    if let controller = storyboard!.instantiateViewController(withIdentifier: "collectionscroll") as? CollectionScrollViewController {
        addChild(controller)
        controller.view.translatesAutoresizingMaskIntoConstraints = false
        middleContainer.addSubview(controller.view)


        NSLayoutConstraint.activate([
            controller.view.leadingAnchor.constraint(equalTo: middleContainer.leadingAnchor),
            controller.view.trailingAnchor.constraint(equalTo: middleContainer.trailingAnchor),
            controller.view.topAnchor.constraint(equalTo: middleContainer.topAnchor),
            controller.view.bottomAnchor.constraint(equalTo: middleContainer.bottomAnchor)
            ])
        controller.didMove(toParent: self)

    }


    if let controller = storyboard!.instantiateViewController(withIdentifier: "collectionscroll") as? CollectionScrollViewController {
        addChild(controller)
        controller.view.translatesAutoresizingMaskIntoConstraints = false
        bottomContainer.addSubview(controller.view)


        NSLayoutConstraint.activate([
            controller.view.leadingAnchor.constraint(equalTo: bottomContainer.leadingAnchor),
            controller.view.trailingAnchor.constraint(equalTo: bottomContainer.trailingAnchor),
            controller.view.topAnchor.constraint(equalTo: bottomContainer.topAnchor),
            controller.view.bottomAnchor.constraint(equalTo: bottomContainer.bottomAnchor)
            ])
        controller.didMove(toParent: self)

    }
因此,为了避免重复代码,我考虑使用looop:

lazy var containers : [UIView] = [topContainer, middleContainer, bottomContainer]

    for container in containers {
        container.translatesAutoresizingMaskIntoConstraints = false
        if let controller = storyboard!.instantiateViewController(withIdentifier: "collectionscroll") as? CollectionScrollViewController {
            addChild(controller)
            controller.view.translatesAutoresizingMaskIntoConstraints = false
            topContainer.addSubview(controller.view)

            NSLayoutConstraint.activate([
                controller.view.leadingAnchor.constraint(equalTo: container.leadingAnchor),
                controller.view.trailingAnchor.constraint(equalTo: container.trailingAnchor),
                controller.view.topAnchor.constraint(equalTo: container.topAnchor),
                controller.view.bottomAnchor.constraint(equalTo: container.bottomAnchor)
                ])
            controller.didMove(toParent: self)
        }
    }
但它不起作用-中间的视图不填充,最后一个视图不滚动


如何在不复制意大利面食代码的情况下填充容器?

我看到一个错误。当您通过添加循环来转换代码时,您忘记将
topContainer
s中的一个更改为
container

更改:

topContainer.addSubview(controller.view)
致:

container.addSubview(controller.view)