更改 NSView 的角半径应该非常简单,但是我收到错误消息“ fatal error :无法展开 Optional.None”。我是否有机会使用 10.9 而不是 10.10 来执行此操作,并且由于框架差异而发生这种情况?或者代码错误。
class aRoundView: NSView {
let cornerRad = 5.0
init(frame: NSRect) {
super.init(frame: frame)
self.layer.cornerRadius = cornerRad
}
override func drawRect(dirtyRect: NSRect)
{
super.drawRect(dirtyRect)
NSColor.redColor().setFill()
NSRectFill(dirtyRect)
}
}
编辑
调用它
func applicationDidFinishLaunching(aNotification: NSNotification?) {
let aView = mainViewTest(frame: NSMakeRect(0, 0, 100, 100))
self.window.contentView.addSubview(aView)
}
其实不止如此。使用 self.layer 的任何迭代都会给出相同的结果、背景颜色等。
最佳答案
那是因为self.layer
是一个可选值,目前没有设置。在 self.layer.cornerRadius
之前添加 self.wantsLayer = true
,以确保存在适当的 layer
。
init(frame: NSRect) {
super.init(frame: frame)
self.wantsLayer = true
self.layer.cornerRadius = cornerRad
}
关于macos - 如何在Swift语言中改变NSView的圆角半径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24038964/