【问题标题】:NSButton Rounded Corners without Subclassing没有子类化的 NSButton 圆角
【发布时间】:2021-12-18 03:31:06
【问题描述】:

无论如何创建一个具有圆角和背景颜色的 NSButton 而不对其进行子类化。我有这段代码,但它什么也没做。

 let directionsButton = NSButton(frame: NSRect(x: 0, y: 0, width: 100, height: 60))
        directionsButton.title = "Click Me!"
        directionsButton.layer?.cornerRadius = 10
        directionsButton.layer?.masksToBounds = true
        directionsButton.layer?.backgroundColor = NSColor.blue.cgColor

【问题讨论】:

    标签: ios cocoa


    【解决方案1】:

    要启用 CoreAnimation 层,您必须在使用前插入此行

    directionsButton.wantsLayer = true
    

    编辑:为了能够更改颜色,您必须绘制一个无边框按钮。添加

    directionsButton.isBordered = false
    

    【讨论】:

    • 谢谢!我添加了 wantLayer 但按钮仍显示文本。我可以看到后面的背景,但没有正确填充。请参阅原始问题中的图片。
    • 请看编辑。
    猜你喜欢
    • 2016-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-22
    相关资源
    最近更新 更多