Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Give NSWindow a background image

Tags:

cocoa

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!

like image 488
Zrb0529 Avatar asked Mar 25 '11 16:03

Zrb0529


2 Answers

I think you should be able to do something like:

[window setBackgroundColor:[NSColor colorWithPatternImage:[NSImage imageNamed:@"myImage.png"]]];
like image 97
Greg Avatar answered Oct 30 '22 12:10

Greg


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.

like image 22
jscs Avatar answered Oct 30 '22 13:10

jscs