无法在NSClipView中正确堆叠视图

时间:2017-01-06 20:25:05

标签: swift nsview nsclipview

我对这段代码感到生气,因为它只是显示了最后一个视图(在它的正确位置)并忘记绘制其他视图......

// Gets a stack of devices
let volumes = getDevices()
var index = 0
for device in volumes {
    let volume = devicePrototype
    // devicePrototype it's a custom view
    volume?.setName(name: device.name) // setting the label for the name
    volume?.setIcon(icon: device.icon) // setting the image for the icon
    // Setting the position of the view (width = 600, height = 105)
    // and all the views have the same size.
    volume?.layer?.frame = CGRect(x: 0, y: index * 105, width: 600, height: 105)
    // Adding the view to the NSClipView (Here's the problem :P)
    devicesStack.addSubview(volume!)
    index += 1
}

你能帮我找到问题吗?

1 个答案:

答案 0 :(得分:0)

我认为你只创建一个视图(设备)并通过循环修改它。您重复引用相同的devicePrototype对象并仅更改属性。请检查一下。

尝试在每次迭代中初始化一个新对象,

let volume = DevicePrototype()