Stackview 不附加第二个视图 [英] Stackview doesn't append second view

查看:37
本文介绍了Stackview 不附加第二个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以编程方式创建了一个堆栈视图,并向它添加了一个我也以编程方式创建的视图.但是当我尝试添加第二个视图时它不起作用.这是我的代码:

I've created a stack view programmatically and I added a view which I've created programmatically too to it. But when I try to add a second view it doesn't work. Here's my code:

@IBOutlet weak var codingScrollView: UIView!
let codeStackView = UIStackView()
var codeViews = [CodeView]()
let codeView1 = CodeView(name: "Lennart", date: "13/05/2002", code: "Just some code")
let codeView2 = CodeView(name: "Nina", date: "01/07/1999", code: "Also some code")

codingScrollView 是我添加到 UIScrollView 的内容视图.codeStackView 是我之前描述过的codeViews 数组用于将视图添加到堆栈视图.

The codingScrollView is the contentview I've added to a UIScrollView. The codeStackView is the one I've described before The codeViews array is being used to add the views to the stackview.

这是 viewDidLoad 方法:

Here's the viewDidLoad method:

    codeViews.append(codeView1)
    codeViews.append(codeView2)

    codingScrollView.addSubview(codeStackView)
    codingScrollView.backgroundColor = UIColor(red: 226/255, green: 226/255, blue: 226/255, alpha: 1)

    codeStackView.centerXAnchor.constraint(equalTo: codingScrollView.centerXAnchor)
    codeStackView.centerYAnchor.constraint(equalTo: codingScrollView.centerYAnchor)

    codeStackView.translatesAutoresizingMaskIntoConstraints = false

    codeStackView.spacing = 10

    codeStackView.axis = .horizontal
    codeStackView.alignment = .center

    for i in 0...codeViews.count - 1 {
        codeStackView.addSubview(codeViews[i])
        codeStackView.addArrangedSubview(codeViews[i])
    }

但如果我运行该应用程序,它不会显示第二个视图,它只会显示其中之一.

But if I run the app it won't show the second view, it only shows one of them.

非常感谢,我真的很感激任何形式的帮助

Thank you very much, I really appreciate any kind of help

推荐答案

尝试先将 codeView{1,2} 添加到堆栈视图,然后将 codeStackView 添加到 codingScrollView.

Try adding the codeView{1,2} to the stack view first then add the codeStackView to the codingScrollView.

另外,在做任何约束之前,一定要设置codeStackView.translatesAutoresizingMaskIntoConstraints = false.

Also, be sure to set codeStackView.translatesAutoresizingMaskIntoConstraints = false before doing any constraints.

codeViews.append(codeView1)
codeViews.append(codeView2)

for i in 0...codeViews.count - 1 {
    codeStackView.addSubview(codeViews[i])
    codeStackView.addArrangedSubview(codeViews[i])
}

codeStackView.translatesAutoresizingMaskIntoConstraints = false

codeStackView.centerXAnchor.constraint(equalTo: codingScrollView.centerXAnchor)
codeStackView.centerYAnchor.constraint(equalTo: codingScrollView.centerYAnchor)

codeStackView.spacing = 10

codeStackView.axis = .horizontal
codeStackView.alignment = .center

codingScrollView.addSubview(codeStackView)
codingScrollView.backgroundColor = UIColor(red: 226/255, green: 226/255, blue: 226/255, alpha: 1)

这篇关于Stackview 不附加第二个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆