0

I am trying to make an image an button (NSButton) in a Cocoa application, below is the code I am trying to use:

[mrButton setImage:(NSImage *)@"button.jpg"];

I have linked mrButton to an NSButton in interface builder and I have a file within my Xcode project called button.jpg and I was wondering how I would go about correcting this code or making an NSButton an image.

Thanks in advance!

VenoMKO
  • 3,294
  • 32
  • 38
  • http://stackoverflow.com/questions/23687118/nsbutton-setimage-not-working-for-me can someone please help me on this ? – Tanzeel May 15 '14 at 19:37

1 Answers1

9

This should work: [mrButton setImage:[NSImage imageNamed:@"button.jpg"]];

VenoMKO
  • 3,294
  • 32
  • 38
  • Thanks a lot, now I'm getting somewhere, do you know how I could remove the actual button (i.e the background and border) and just have the image act as an NSButton? –  Aug 03 '11 at 16:02
  • 1
    You are welcome. You can subclass NSButtonCell and rewrite `drawBezelWithFrame:inView:` method. Here are [Apple's docs](http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Button/Button.html#//apple_ref/doc/uid/10000019i) – VenoMKO Aug 03 '11 at 16:07
  • Just found that there's an easier way to remove: [mrButton setBordered:NO]; –  Aug 03 '11 at 16:46
  • 1
    Yeah, but try to press this button. I've ended up with awkward looking while its pressed. – VenoMKO Aug 03 '11 at 16:51
  • Yea, it does look somewhat weird when pressed... Thanks anyway for your help! –  Aug 03 '11 at 17:10
  • I had to remove the file type extensions '.jpg' for this to work, otherwise the button was black. The images were already loaded into 'Assets.xcassets' where the file extensions are automatically stripped out. – Pierre Dufresne Jun 06 '21 at 15:59