Swift:NSStackView,仅添加一个子视图的错误

时间:2019-05-21 04:37:35

标签: swift cocoa

您好,我想在下面的代码中将NSImageView添加到我的stackView中,但是存在一个错误,因为仅添加了一个。循环是3次迭代,所以通常我应该有3张图片:

    let imageView = NSImageView(frame: NSRect(x: 0, y: 0, width: 50, height: 50))
    imageView.image = image.image

    icons.forEach { _ in
        stackImage.addArrangedSubview(imageView)
    }

    print(stackImage.subviews.count) // Outpout 1

1 个答案:

答案 0 :(得分:2)

在forloop中创建NSImageView实例。 而且您需要检查stackImage.arrangedSubviews.count而不是stackImage.subviews.count

var icons = [NSImage(named: ""),NSImage(named: ""),NSImage(named: "")]
icons.forEach { image in
    let imageView = NSImageView(frame: NSRect(x: 0, y: 0, width: 50, height: 50))
    imageView.image = image
    stackImage.addArrangedSubview(imageView)
}
print(stackImage.arrangedSubviews.count)