奇怪的NSButton创作

时间:2015-02-19 15:49:53

标签: swift nsbutton

我有一个像这样简单的AppDelegate:

class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {

  @IBOutlet weak var window: NSWindow!
  @IBOutlet weak var field: NSView!

  let button = NSButton()
  let buttons = [NSButton(), NSButton(), NSButton()]

  func applicationDidFinishLaunching(aNotification: NSNotification) {
    button.frame = NSMakeRect(30, 60, 80, 25)
    button.title = "ok"
    field.addSubview(button)

    for index in 0...2 {
      let buttonI = buttons[index]
      buttonI.frame = NSMakeRect(30+CGFloat(index)*90, 30, 80, 25)
      buttonI.title = "not ok:\(index)"
      field.addSubview(buttonI)
    }
  }
}

(您可以通过在窗口的内容视图中将field与自定义视图相关联来测试上述内容。)

当我运行时,我得到以下按钮外观:

enter image description here

为什么单个按钮看起来没问题而数组中的按钮看起来没有?

1 个答案:

答案 0 :(得分:1)

您只需要设置bezelStyle:

button.bezelStyle = .RoundedBezelStyle

buttonI.bezelStyle = .RoundedBezelStyle