UIStackView - 安排好的视图是重叠的。

问题描述 投票:0回答:1

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

然后在代码中,我有下面的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的时候,我才能真正看到是多个视图。

我试着将 "translateAutoresizingMaskIntoConstraints "设置为false,我试着为自定义视图的框架设置硬编码值,为堆栈设置不同的约束,甚至从scrollview中删除它。但是什么都没有用。

PS我试过了我在网上看到的几个解决方案。还是什么都没有。

谢谢你了。

swift xcode constraints uistackview
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)
}
© www.soinside.com 2019 - 2024. All rights reserved.