【问题标题】:How to change Corner Radius of NSView in Swift Language如何在 Swift 语言中改变 NSView 的圆角半径
【发布时间】:2014-07-25 04:38:44
【问题描述】:

更改 NSView 的角半径应该非常简单,但是我收到错误消息“致命错误:无法打开 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.layernil。它是图层支持的视图吗?在通话之前尝试self.wantsLayer = true

标签: macos cocoa nsview swift


【解决方案1】:

那是因为self.layer 是一个可选值,目前没有设置。在self.layer.cornerRadius 之前添加self.wantsLayer = true,以确保存在正确的layer

init(frame: NSRect) {
    super.init(frame: frame)
    self.wantsLayer = true
    self.layer.cornerRadius  = cornerRad
}

【讨论】:

    猜你喜欢
    • 2014-06-12
    • 1970-01-01
    • 2016-03-18
    • 1970-01-01
    • 2014-08-29
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 2016-03-28
    相关资源
    最近更新 更多