LoginSignup
0
0

More than 5 years have passed since last update.

Swift:押した時に画像が変わるNSButton

Posted at
let btn = NSButton(frame: NSRect(x: 5, y: 5, width: 60, height: 40))

override func awakeFromNib() {
    super.awakeFromNib()
    btn.title = ""
    btn.image = NSImage(named: "image.png")
    btn.alternateImage = NSImage(named: "highlighted_image.png")
    btn.imageScaling = NSImageScaling.scaleProportionallyDown
    //重要なのはこれ↓
    btn.setButtonType(NSButton.ButtonType.momentaryChange)

    // btn.wantsLayer = true
    // btn.layer?.cornerRadius = 12
    // btn.isBordered = false

    btn.target = self
    btn.action = #selector(push)
    addSubview(btn)
}                

@objc func push() {
    print("hey")
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0