Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 以编程方式逐个添加视图_Ios_Swift_Uiscrollview_Autolayout - Fatal编程技术网

Ios 以编程方式逐个添加视图

Ios 以编程方式逐个添加视图,ios,swift,uiscrollview,autolayout,Ios,Swift,Uiscrollview,Autolayout,我是Swift新手(使用4版)。 我有HTML文本,里面可以包含图片链接 some text <img src=""/> some text 编辑: 代码似乎是正确的,视图添加正确,但约束不允许滚动视图 我测试了约束(中心Y-向它添加了200多个范围,它开始滚动,但它是硬编码的值,所以它可能有更合法的解决方案?) 如果没有这种约束,它就会产生错误,根本不起作用 我认为这条线路是你的麻烦之源: lastView.bottomAnchor.constraint(equalTo: co

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

some text 
<img src=""/>
some text
编辑: 代码似乎是正确的,视图添加正确,但约束不允许滚动视图

我测试了约束(中心Y-向它添加了200多个范围,它开始滚动,但它是硬编码的值,所以它可能有更合法的解决方案?) 如果没有这种约束,它就会产生错误,根本不起作用


我认为这条线路是你的麻烦之源:

lastView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: 0.0)
现在,当您遍历项目时,您将约束每个标签/图像视图,使其底部锚定约束到
containerView
的底部。我希望这会导致不可满足的约束警告(检查控制台)。要约束添加到容器中的最后一个标签/图像视图的底部。因此,只需将该行从它后面的
循环中移出即可:

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

如果按照这个答案进行重构并不能解决滚动问题,我建议检查一下
containerView
scrollView
之间的约束,您可以将其作为参考。

我相信这一行是您的问题根源:

lastView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: 0.0)
现在,当您遍历项目时,您将约束每个标签/图像视图,使其底部锚定约束到
containerView
的底部。我希望这会导致不可满足的约束警告(检查控制台)。要约束添加到容器中的最后一个标签/图像视图的底部。因此,只需将该行从它后面的
循环中移出即可:

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

如果按照这个答案进行重构不能解决滚动问题,我建议检查
containerView
scrollView
之间的约束-您可以将其用作参考。

也许更简洁的方法是在scrollView中使用
UIStackView
,这样您可以将任何其他视图作为子视图添加到
UIStackView
。这样,您就不必自己处理约束

因此,视图层次结构应该类似于下面的内容

->UIView
    ->UIScrollView
        ->UIView
            ->UIStackView
然后将约束添加到scrollView中,作为左上右下

还将另一个
UIView
添加到
UIScrollView
作为包装视图,并将约束添加为左上右下,并在此包装视图中添加两个约束,即相对于主视图的等于宽度等于高度。这将使
UIScrollView
与动态高度一起工作,更多信息请参见本节

现在将约束添加到堆栈视图中,如左上右下还将添加带有出口连接的高度约束


现在,当您将任何视图添加到堆栈视图时,只需更改
UIStackView
的高度常数,您的
UIScrollView
将自动展开。

也许更干净的方法是在滚动视图中使用
UIStackView
,这样您就可以将任何其他视图作为子视图添加到堆栈视图中
UIStackView
。这样,您就不必自己处理约束

因此,视图层次结构应该类似于下面的内容

->UIView
    ->UIScrollView
        ->UIView
            ->UIStackView
然后将约束添加到scrollView中,作为左上右下

还将另一个
UIView
添加到
UIScrollView
作为包装视图,并将约束添加为左上右下,并在此包装视图中添加两个约束,即相对于主视图的等于宽度等于高度。这将使
UIScrollView
与动态高度一起工作,更多信息请参见本节

现在将约束添加到堆栈视图中,如左上右下还将添加带有出口连接的高度约束


现在,当您将任何视图添加到堆栈视图时,只需更改
UIStackView
的高度常数,您的
UIScrollView
将自动展开。

使用
UIWebview
WKWebView
显示htmlIt不提供任何定制功能使用
UIWebview
WKWebView
show htmlIt未提供任何定制可能性“label.topAnchor.constraint(equalTo:containerView.topAnchor,常量:0.0)与label.topAnchor.constraint(equalTo:lastView.bottomAnchor,常量:0.0)”相同”。之所以会发生这种情况,是因为containerView在该时刻的高度为0,而此时底部和顶部相等。我想,当我在里面添加一些东西时,容器的底部和顶部会有所不同。它只记得底部(和顶部)的位置吗?即使底部改变了,点也会一样吗?@Vadim哦,我刚刚注意到它的底部和顶部,我的错误。。你以前的代码是正确的,我的是错误的,我将编辑答案ASAP@Vadim好的,我修正了答案。。很好,你怀疑了我的代码:)谢谢)代码现在很好(我编辑了一点帖子,做了一些修改)并且约束“中心Y”不允许我滚动(它会自动向后滚动):(在没有该约束的情况下,滚动根本不工作。@Vadim不要在scrollView中对containerView使用centerY和centerX。将containerView的左右定位约束到与约束scrollView的左右相同的视图(但不是scrollView本身),并约束scrollView的顶部和底部到顶部和底部的锚定-参考此答案:虽然我认为在x轴的情况下,也可以使用centerX+宽度锚定,但绝对不能用于y轴…”label.topAnchor.constraint(equalTo:containerView.topAnchor,常量:0.0)与label.topAnchor.constraint相同(equalTo:lastView.bottomAnchor,常量:0.0)”。发生这种情况是因为containerView在那个时刻的高度为0,而底部和顶部在那个时候相等。我想,当我添加一些东西时