Laurent Crivello
Laurent Crivello

Reputation: 3931

Unable to properly position NSImageView in NSView

I would like to display an NSImageView in my main View:

img=[[NSImageView alloc]init];
[img setImage:[[NSImage alloc]initWithContentsOfFile:filename]];
width=img.image.size.width/2;
height=img.image.size.height/2;
[img setFrame:NSMakeRect(0,0,width ,height)];
mainview = [((AppDelegate *)[NSApp delegate]).window contentView];
[mainview addSubview:img];

The image is properly displayed, however instead of being in the top-left corner of my main window, it's completely going to the right side of the screen.

What is wrong above?

Upvotes: 0

Views: 1341

Answers (1)

Hussain Shabbir
Hussain Shabbir

Reputation: 15015

Try like this:-

- (void)windowDidLoad
{
 NSString *imageName = [[NSBundle mainBundle] pathForResource:@"yourImage ofType:@"tiff"];
    NSImage *photoImage = [[NSImage alloc] initWithContentsOfFile:imageName];
    [yourimgView setImage:photoImage];
    CGFloat width=yourimgView.bounds.size.width;
    CGFloat height=yourimgView.bounds.size.height;
    CGFloat xboundspoint=yourimgView.bounds.origin.x;
    CGFloat yboundspoint=yourimgView.bounds.origin.y;   
    [yourimgView setFrame:NSMakeRect(xboundspoint, yboundspoint+150.0, width, height)];
    [yourview addSubview:yourimgView];
    [[[self window]contentView]addSubview:yourview];
    [super windowDidLoad];
}

Upvotes: 1

Related Questions