所有这些似乎都是过时的,因为NSControl中的所有单元格方法都是deprecated in Yosemite.我没有发现任何建议或指导用作替代物。
这是唯一的声明,我可以找到:
Gradual deprecation of NSCell
Mac OS X 10.10 takes another step towards the eventual deprecation of
cells. Direct access to the cell of a control is discouraged,and
methods which allow it will be formally deprecated in a subsequent
release. A variety of cell-level APIs have been promoted to varIoUs
Control subclasses in order to provide cell-free access to important
functionality. NSLevelIndicator,NSTextField,NSSearchField,NSSlider,
and NSPathControl all have new properties for this purpose. Cell-based
NSTableViews are now deprecated,and view-based NSTableViews should be
used instead. Matrix-based NSBrowsers are also deprecated in favor of
the item-based interface.Excerpt from: 07002
没有关于NSButton的话。
NSTextField支持层支持的视图;因为这样,我在我的NSButton上尝试了同样的方法,但是没有效果。
var btn = NSButton(NSMakeRect(0,50,20)) btn.wantsLayer = true btn.bordered = false btn.layer?.backgroundColor = NSColor(calibratedWhite: 0.99,alpha: 1).CGColor btn.layer?.borderWidth = 1 btn.layer?.borderColor = NSColor(calibratedWhite: 0.81,alpha: 1).CGColor
更重要的是,您可以提到动画。
从我正在开发的项目中提取一些代码(自定义NSButton):
从init()…
self.wantsLayer = true self.layerContentsRedrawPolicy = NSViewLayerContentsRedrawPolicy.OnSetNeedsDisplay self.layer?.borderColor = NSColor.gridColor().CGColor self.layer?.borderWidth = 0.5 self.layer?.backgroundColor = NSColor.whiteColor().CGColor
然后可以在特定于层的显示循环中获得细粒度控制:
override var wantsUpdateLayer:Bool{ return true } override func updateLayer() { // your code here super.updateLayer() }
如果您的自定义按钮需要一个形状,那么您甚至可以使用下面的CAShapeLayer来使您的背衬层成为一个特殊的形状…更多的细节需要研究。
override func makeBackingLayer() -> CALayer { return CAShapeLayer() }