ios - 以编程方式一个接一个地添加 View

标签 ios swift uiscrollview autolayout

我是 Swift 的新手(使用 4 版本)。 我有 HTML 文本,里面可以包含图片链接。

some text 
<img src=""/>
some text

我想让内容可以滚动,所以我将容器(简单的 UIView)包装到 UIScrollview 中。

然后我将 HTML 文本解析为多个部分:“文本”和“图像链接”到一个数组(数据和类型)中。 然后遍历我观察的那个数组,如果类型是文本——我输入带有约束的标签:

如果它是容器的第一个元素,则左、上、右和下匹配。 如果不是,则顶部与容器(如链)中的前一个 View 的底部相匹配。

但由于某种原因,它不起作用。 我究竟做错了什么? 我把我的代码:

    var lastView = containerView!
    var label : UILabel
    var imageView : UIImageView

    for item in articleContent {
        if (item.type == RenderDetailBlogObject.TYPE_TEXT) {
            label = UILabel()

            label.numberOfLines = 0
            label.attributedText = item.data.html2AttributedString
            containerView.addSubview(label)

            label.translatesAutoresizingMaskIntoConstraints = false

            NSLayoutConstraint.activate([
                label.leftAnchor.constraint(equalTo: containerView.leftAnchor),
                label.rightAnchor.constraint(equalTo: containerView.rightAnchor),
                ])

            if (lastView == containerView) {
                print("ADDING LABEL lastView == containerView")
                label.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 0.0).isActive = true
            } else {
                  print("ADDING LABEL lastView != containerView")
                 label.topAnchor.constraint(equalTo: lastView.bottomAnchor, constant: 0.0).isActive = true
            }

            lastView = label
        } else {
            imageView = UIImageView()

            imageView.sd_setImage(with: URL(string: item.data))
            containerView.addSubview(imageView)

            imageView.translatesAutoresizingMaskIntoConstraints = false

            NSLayoutConstraint.activate([

                imageView.leftAnchor.constraint(equalTo: containerView.leftAnchor),
                imageView.rightAnchor.constraint(equalTo: containerView.rightAnchor),
                ])

            if (lastView == containerView) {
                print("ADDING AN IMAGE lastView == containerView")
                 imageView.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 0.0).isActive = true
            } else {
                 print("ADDING AN IMAGE lastView != containerView")
                imageView.topAnchor.constraint(equalTo: lastView.bottomAnchor, constant: 0.0).isActive = true
            }

            lastView = imageView
        }

        lastView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: 0.0)
    }

编辑: 代码似乎是正确的并且 View 正在正确添加,但限制不允许 ScrollView 。

我测试了约束(中心 Y - 增加了 200 个范围并开始滚动,但它是硬编码值,所以它可能有更合法的解决方案?) 如果没有该约束,它就会出错并且根本无法工作。

enter image description here

最佳答案

我相信这条线是你的麻烦之源:

lastView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: 0.0)

现在,当您遍历项目时,您将每个标签/ ImageView 约束为将底部 anchor 约束到 containerView 的底部。我希望这会导致 Unsatisfiable constraints 警告(检查你的控制台)。您想要限制添加到容器中的最后一个标签/ ImageView 的底部。因此,只需将该行移出它后面的 for 循环即可:

var lastView = containerView!
var label : UILabel
var imageView : UIImageView

for item in articleContent {
    if (item.type == RenderDetailBlogObject.TYPE_TEXT) {
        label = UILabel()

        label.numberOfLines = 0
        label.attributedText = item.data.html2AttributedString
        containerView.addSubview(label)

        label.translatesAutoresizingMaskIntoConstraints = false

        NSLayoutConstraint.activate([
            label.leftAnchor.constraint(equalTo: containerView.leftAnchor),
            label.rightAnchor.constraint(equalTo: containerView.rightAnchor),
            ])

        if lastView == containerView {
            print("ADDING LABEL lastView == containerView")
            label.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 0.0).isActive = true
        } else {
              print("ADDING LABEL lastView != containerView")
            label.topAnchor.constraint(equalTo: lastView.bottomAnchor, constant: 0.0).isActive = true
        }

        lastView = label
    } else {
        imageView = UIImageView()

        imageView.sd_setImage(with: URL(string: item.data))
        containerView.addSubview(imageView)

        imageView.translatesAutoresizingMaskIntoConstraints = false

        NSLayoutConstraint.activate([

            imageView.leftAnchor.constraint(equalTo: containerView.leftAnchor),
            imageView.rightAnchor.constraint(equalTo: containerView.rightAnchor),
            ])

        if lastView == containerView {
            print("ADDING AN IMAGE lastView == containerView")
            imageView.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 0.0).isActive = true
        } else {
            print("ADDING AN IMAGE lastView != containerView")
            imageView.topAnchor.constraint(equalTo: lastView.bottomAnchor, constant: 0.0).isActive = true
        }

        lastView = imageView
    }
}
// Constrain the bottom only for the last view that was added
lastView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: 0.0).isActive = true

如果根据这个答案重构不能解决滚动问题,我建议检查 containerViewscrollView 之间的约束 - 你可以使用 this answer作为引用。

关于ios - 以编程方式一个接一个地添加 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48495425/

相关文章:

ios - react native : "setBackgroundColor is only available on android" warning on iOS

ios - UISwitch 无法正常工作

ios - Firebase swift 错误 Storage.storage() 在范围内找不到 'Storage'

swift - 如何通过 mobile hub 和 Swift 设置 DynamoDB Table?

objective-c - 如何从 ScrollView 中删除 subview ?

ios - UIScrollView contentOffset 在切换到前台时自动设置

ios - Swift:声明一个空字典

javascript - 如何防止两个react-native Picker相互影响?

ios - 无法在另一个类中设置 UIButton 目标?

ios - 如何以编程方式在 ScrollView 中嵌入堆栈 View