gpt4 book ai didi

swift - 循环添加到 ContainerViews

转载 作者:行者123 更新时间:2023-11-28 13:29:29 25 4
gpt4 key购买 nike

我想使用容器 View 来包含一个 Collection View 的三个实例。

我的网点是:

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

}

所以为了减少重复代码,我想到了使用循环:

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)
}
}

但它不起作用 - 中间 View 不填充,最后一个不滚动。

如何在不复制 pasta codez 的情况下填充我的容器?

最佳答案

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

改变:

topContainer.addSubview(controller.view)

到:

container.addSubview(controller.view)

关于swift - 循环添加到 ContainerViews,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57637384/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com