Zrb0529
Zrb0529

Reputation: 800

Give NSWindow a background image

Ok, so I've created an image in Photoshop that will align with the buttons on my app, and now I'd like to make it the background image of my window so that the characters on the image will correspond to the keys on my app (a small calculator demo app I've been working on)

Basically, instead of giving buttons Text like 1,2,3,4, etc. I've made a 3x3 map with numbers of a different font just because it will look pretty

What I'm having difficulty with now is that I can't seem to make the image the background of my window.

I created an NSImageView and I dragged the image file onto it, so I can see it now, but I can't make it the background.

Do I need to subclass the NSImageView or is there some simple method?

I'm using XCode 4, btw

Thanks!

Upvotes: 7

Views: 8200

Answers (3)

Richard
Richard

Reputation: 3376

Just to give you more options -- you should be able to [window setContentView:myImageView]. In the .xib file you'd want to add your buttons etc. as subviews of the image view.
I don't necessarily recommend this approach, but it's something to think about.

Upvotes: 1

jscs
jscs

Reputation: 64002

This is something that a layer-backed window would be good at:

[[window contentView] setWantsLayer:YES];
[[window contentView] layer].contents = myImage;

I think you stand a better chance of getting this to resize sensibly (assuming you need to) than with a pattern color.

Upvotes: 8

Greg
Greg

Reputation: 33650

I think you should be able to do something like:

[window setBackgroundColor:[NSColor colorWithPatternImage:[NSImage imageNamed:@"myImage.png"]]];

Upvotes: 14

Related Questions