如何在Swift语言中更改NSView的转角半径 [英] How to change Corner Radius of NSView in Swift Language
问题描述
更改NSView的拐角半径应该很简单,但是我收到错误消息严重错误:无法解开Optional.None".我是否有可能用10.9而不是10.10进行此操作,这是由于框架差异而发生的吗?或代码错误.
changing corner radius of NSView should be pretty straight forward however i am getting error message "fatal error: Can't unwrap Optional.None". is there a chance i am doing this with 10.9 not 10.10 and this is happening due framework differences? or the code is wrong.
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进行的任何迭代都会得到相同的结果,backgroundcolor等.
actually it is not just that. any iteration with self.layer gives same result, backgroundcolor etc.
推荐答案
这是因为self.layer
是一个可选值,当前未设置.在self.layer.cornerRadius
之前添加self.wantsLayer = true
,以确保存在正确的layer
.
That is because self.layer
is an optional value, which is currently not set. Add self.wantsLayer = true
before self.layer.cornerRadius
, to make sure a proper layer
exists.
init(frame: NSRect) {
super.init(frame: frame)
self.wantsLayer = true
self.layer.cornerRadius = cornerRad
}
这篇关于如何在Swift语言中更改NSView的转角半径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!