0
votes

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?

1
Auto layout makes this easieruchuugaka
The point is that I would like to keep full control of the positioning. So should I remove the auto layout, or add it ? Any suggestion how ? Thanks.Laurent Crivello
If you are using auto layout, stop trying to touch frames. Just set constraints. Add the constraints to the superview AFTER adding your view as a subview. You may need to call layout or layoutIfNeeded on the superview after adding the constraints.uchuugaka
I am not using auto-layout because I would like to keep full control on positions. How can I get an event everytime the Window is moved/resized to redraw the nsimageview at the right position ?Laurent Crivello
Auto layout gives you control.uchuugaka

1 Answers

1
votes

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];
}