Frage

I have been working on iPhone for some time and just recently I have been trying my hands on a Mac app. But it is giving me a hard time. I want to add an image over my window from coding. I have done following code but it doesn't work:

NSImageView *imView = [[NSImageView alloc] initWithFrame:NSMakeRect(250, 50, 180, 160)];
NSImage *myImage = [[NSImage alloc] initByReferencingFile:@"top.png"];
[imView setImage:myImage];
[window setContentView:imView];
War es hilfreich?

Lösung

ok I found my mistake. I was adding it to window instead I should have added it to content view. I changed my code to following and it worked:

[self.window.contentView addSubview:imView];

Andere Tipps

You need to specify the path to the image file. Assuming it is in your main bundle, would you try using:

NSImage *myImage = [NSImage initByReferencingFile:[[NSBundle mainBundle] pathForImageResource:@"top.png"]];
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top