【问题标题】:Can't stack views correctly in a NSClipView无法在 NSClipView 中正确堆叠视图
【发布时间】:2017-01-06 20:25:05
【问题描述】:

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

// 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
}

你能帮我找出问题吗?

【问题讨论】:

    标签: swift nsview nsclipview


    【解决方案1】:

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

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

    let volume = DevicePrototype()
    

    【讨论】:

    • 它不起作用,因为我在界面构建器中使用自定义视图创建了界面,并使用插座将其标签和图像视图连接到其自己的类。所以重点是:如果我只初始化类,我将无法使用接口:(我的解决方案是从应用程序委托(名为 devicePrototype)的自定义视图中创建一个出口,这样我就可以创建像这样的实例这个...如何在界面构建器中创建多个构建的实例?
    • 创建自定义视图类并使用其实例。您可以将这些类连接到您的界面构建器视图。
    猜你喜欢
    • 2020-04-24
    • 1970-01-01
    • 2016-04-16
    • 1970-01-01
    • 1970-01-01
    • 2019-03-08
    • 2014-02-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多