Cocoa frame window background image

I am looking for the perfect solution to set the background image for a window in a cocoa application. I have not found a solution for this, I am new to c lens, so please help me ...

+6
objective-c cocoa
source share
2 answers

A window in Cocoa has a root-level view called "view content." This is the view that contains everything else in the window. By default, it is just empty, NSView . But you can easily create your own subclass of NSView , override the drawRect: method to draw a background image, and use it for your custom view.

However, it's easier to just use the plain old NSImageView . The advantage of this is that you can set, for example, autorun behavior to save the image attached to one corner (try this using Installer.app by resizing the installer window). You can also make it translucent so that the background appears a little. (Again, I think of Installer.app, your application may be completely different)

Hope you go in the right direction!

+5
source share

Michael Vannorsl suggests faking an NSView for this purpose, and I quote:

You really would be better off making a Subclass of NSView and drawing it the image you want in drawRect :.

 - (void)awakeFromNib { myImage = [[NSImage alloc] init.... [self setNeedsDisplay:YES]; } - (void)drawRect:(NSRect)rect { NSSize isize = [myImage size]; [myImage drawInRect:[self bounds] fromRect:NSMakeRect(0.0, 0.0, isize.width, isize.height) operation: NSCompositeCopy fraction:1.0]; } 

Read all this thread on cocoabuilder , this is pretty instructive.

+3
source share

All Articles