0

在过去的几个小时里,我一直在努力将自定义视图添加到 UIStackView。StackView 放置在 UIScrollView 内,并为 ScrollView 的每个边距设置了约束。一切都是故事板中的设计。

然后在代码中我有以下 for 循环应该将我的自定义视图添加到堆栈中:

for name in names {
    let initialChildProfile = ChildProfileView.loadFromNibNamed(nibNamed: "ChildProfileView") as! ChildProfileView
    initialChildProfile.frame = CGRect(x: 0, y: 0, width: childrenStackOutlet.frame.size.width, height: initialChildProfile.frame.size.height)
    initialChildProfile.isUserInteractionEnabled = true

    childrenStackOutlet.addArrangedSubview(initialChildProfile)
}

我之前已经这样做了很多次,一切都很好,但是这次自定义视图相互重叠。只有当我将间距设置为大于 0 时,我才能真正看到超过 1 个视图。

我尝试将“translateAutoresizingMaskIntoConstraints”设置为false,我尝试为自定义视图的框架设置硬编码值,对堆栈设置不同的约束,甚至将其从滚动视图中删除。但没有任何效果。

PS我尝试了我在网上看到的一些解决方案。依然没有。

谢谢你。

4

1 回答 1

1

问题是自动布局

for name in names {
    let initialChildProfile = ChildProfileView.loadFromNibNamed(nibNamed: "ChildProfileView") as! ChildProfileView
    initialChildProfile.translatesAutoresizingMaskIntoConstraints = false
    initialChildProfile.widthAnchor.constraint(equalToConstant: childrenStackOutlet.frame.size.width).isActive = true
    //initialChildProfile.heightAnchor.constraint(equalToConstant: self.view.frame.width - 50).isActive = true
    initialChildProfile.isUserInteractionEnabled = true

    childrenStackOutlet.addArrangedSubview(initialChildProfile)
}
于 2020-04-29T06:03:32.670 回答