Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.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
Swift 如何将偏移复选框列表添加到NSScrollView?_Swift_Macos - Fatal编程技术网

Swift 如何将偏移复选框列表添加到NSScrollView?

Swift 如何将偏移复选框列表添加到NSScrollView?,swift,macos,Swift,Macos,我有一个NSScrollView,我正试图用复选框NSButton/NSToggleButton填充它。我添加的复选框数应等于数组中的项目数。这部分工作正常 下面的代码成功地将复选框添加到滚动视图中,但它们都位于彼此的顶部。我试图添加它们,但以列表视图的方式相互偏移,例如iOS上的tableview。这是我的密码: for calendar in self.allCalendars { self.allCalendars.append(calendar as EKCalendar)

我有一个NSScrollView,我正试图用复选框NSButton/NSToggleButton填充它。我添加的复选框数应等于数组中的项目数。这部分工作正常

下面的代码成功地将复选框添加到滚动视图中,但它们都位于彼此的顶部。我试图添加它们,但以列表视图的方式相互偏移,例如iOS上的tableview。这是我的密码:

for calendar in self.allCalendars {
     self.allCalendars.append(calendar as EKCalendar)        
     let checkbox = NSButton.init(checkboxWithTitle: calendar.title, target: self, action: #selector(self.checkboxDidChange))
     checkbox.setButtonType(NSToggleButton)
     self.checkboxArray.append(checkbox as NSButton)
     self.addSubview(checkbox)       
}
下面是结果的屏幕截图:


下面是一些您可以实现的东西,它将改变您执行循环的方式,这样您就有了每个迭代的索引,但它可以帮助您计算某种垂直间距:

for i in 0 ..< allCalendars.count {

    let margin: CGFloat = 5.0   // Or whatever you want the padding on the left to be

    let vertSpacing: CGFloat = 10.0    // However much separation you want in between boxes

    self.allCalendars.append(allCalendars[i] as EKCalendar)
    let checkbox = NSButton.init(checkboxWithTitle: allCalendars[i].title, target: self, action: #selector(self.checkboxDidChange))
    checkbox.setButtonType(NSToggleButton)

    // Now you can set the frame of the checkbox within it's superview's coordinate system
    // This calculates it based on the bounds.height, if you want to use a custom height, substitute that.
    checkbox.frame = CGRect(x: margin, y: vertSpacing + (vertSpacing + checkbox.bounds.height)*CGFloat(i), width: checkbox.bounds.width, height: checkbox.bounds.height)

    self.checkboxArray.append(checkbox as NSButton)
    self.addSubview(checkbox)

}

基本上,y值vertSpacing+vertSpacing+checkbox.bounds.height*CGFloati所做的是,它从第一个复选框开始,以y-pos表示您设置的垂直间距值,然后对于之后的每个复选框,它将在上一个复选框的下方设置一个垂直间距值。此解决方案将假定每个复选框的高度相同。如果不想使用bounds.height值,可以设置自定义高度。

您看过NSTableView吗?没有。它看起来更适合实现我在这里尝试的目标。这是我的第一个osx应用程序,我不知道这是可用的。显然,获得我目前实现的东西是我的第一选择,但NSTableView看起来是个不错的选择。你以前做过iOS编程吗?每个按钮都有一个左上角为0,0的框架,因此它们都位于彼此的顶部。您需要在添加帧时调整帧。如果您不想做帧计算,也可以查看NSStackView。对不起,您以前做过iOS编程吗?听起来有点屈尊俯就。我只是想评估一下你对这个话题的熟悉程度。你为什么不使用NButton.initframe:xxx?成功了!非常感谢。我只需要在循环中添加let calendar=self.allCalendars[I],这样我就可以得到calendar.titleoh!对不起,我刚把白板的样式编码了。我将为未来的人编辑我的答案。很高兴它对你有用