Creating NSImageView programmatically [fixed]
I'm trying to create a splash screen by bringing up an NSWindow and putting an image on it – programmatically. I've got a good reason not to use a .nib file, so bear with me.
I get the window alright, but the image doesn't show up. I've never worked with NSImage or NSImageReps, so perhaps I'm missing something?
If anyone could point me in some sort of direction, it'd be much appreciated.
[EDIT: Fixed it, I was using [NSImageView init] instead of [NSImageView initWithFrame:].]
I get the window alright, but the image doesn't show up. I've never worked with NSImage or NSImageReps, so perhaps I'm missing something?
Code:
splashWindow = [[NSWindow alloc] initWithContentRect: NSMakeRect (0, 0, 400, 400)
styleMask: NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO];
NSImage *imageFromBundle = [NSImage imageNamed:@"splashscreen.png"];
NSImageView *imageView = [[NSImageView alloc] init];
[imageView setImage: imageFromBundle];
[[splashWindow contentView] addSubview: imageView];
[imageView setBounds: NSMakeRect (0, 0, 256, 256)];
[splashWindow setHasShadow: YES];
[splashWindow center];
[splashWindow makeKeyAndOrderFront: self];
[imageView display];If anyone could point me in some sort of direction, it'd be much appreciated.
[EDIT: Fixed it, I was using [NSImageView init] instead of [NSImageView initWithFrame:].]
What on earth good reason do you have not to use a nib file?
OK, scratch "good" reason, but here are some:
- I'm building a custom-shaped window, so I'll use the image view to knock out the silhouette. Doesn't make sense to build a opaque NSWindow in IB, then.
- My resource handling script doesn't play well with including images in nib files, so I couldn't put the image on the window in IB.
- There'll be some custom animation done on the window, reshaping and moving it in a way that didn't make sense to setup in IB
- Worst of all: there's no way to create a borderless window in IB.
- I'm building a custom-shaped window, so I'll use the image view to knock out the silhouette. Doesn't make sense to build a opaque NSWindow in IB, then.
- My resource handling script doesn't play well with including images in nib files, so I couldn't put the image on the window in IB.
- There'll be some custom animation done on the window, reshaping and moving it in a way that didn't make sense to setup in IB
- Worst of all: there's no way to create a borderless window in IB.
yeah, the no-borderless-windows-in-IB thing is a good reason... though it hasn't stopped me 
I've created an AlwaysBorderlessWindow class that's a subclass of NSWindow, overriding the two -initWith........ methods to change the style mask. Works fine.

I've created an AlwaysBorderlessWindow class that's a subclass of NSWindow, overriding the two -initWith........ methods to change the style mask. Works fine.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Can you programmatically set the title of a NStextfield? | kensuguro | 5 | 4,213 |
Sep 6, 2005 08:09 AM Last Post: Steven |
|

