【问题标题】:UIStackView - arranged views are overlappingUIStackView - 排列的视图重叠
【发布时间】:2020-04-29 04:09:10
【问题描述】:

过去几个小时我一直在努力将自定义视图添加到 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 我尝试了我在网上看到的几个解决方案。还是什么都没有。

谢谢。

【问题讨论】:

    标签: swift xcode constraints uistackview


    【解决方案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)
    }
    

    【讨论】:

    • 你是对的。使用你的约束我不再有问题,但我必须同时使用约束,宽度和高度。现在的问题是自定义视图将根据内容具有不同的高度,所以我不能真正使用高度值......你有什么想法吗?
    • 如果您的自定义视图有不同的高度,您必须在我的代码中删除 heightAncher 并在您的自定义视图中使用自动布局,之后堆栈视图高度是动态的,取决于您的自定义视图高度
    猜你喜欢
    • 2016-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-24
    相关资源
    最近更新 更多