0

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?

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Laurent Crivello
  • 3,809
  • 6
  • 45
  • 89
  • Auto layout makes this easier – uchuugaka Sep 19 '13 at 00:58
  • 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 Sep 19 '13 at 05:52
  • 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 Sep 19 '13 at 07:00
  • 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 Sep 19 '13 at 19:15
  • Auto layout gives you control. – uchuugaka Sep 19 '13 at 22:42
  • Good to know ! Any suggestion on how to set the constraints to, for example, have the nsimageview always located at the top left corner ? I could not find the right arguments to AddConstraints. Thanks. – Laurent Crivello Sep 20 '13 at 07:12
  • Rule is constraints need to be enough in vertical and horizontal directions to be satisfiable. Not necessarily precise. – uchuugaka Sep 20 '13 at 08:23
  • Would you have a concrete example of code of AddConstraints command to add a constraint to an NSImageView ? Thanks – Laurent Crivello Sep 20 '13 at 13:15

1 Answers1

1

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];
}
Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56
  • Thanks, but this one gives me the same issue. What I found out however is that as soon as I change the window size using the mouse, the picture goes directly to the right location. Any idea how to have it going directly to the right position ? – Laurent Crivello Sep 19 '13 at 18:38
  • For this no need to worry select your imageview and then in Interface builder inside size inspector check all the line of autosizing then your image view cannot move anywhere whenever you resize your window. – Hussain Shabbir Sep 20 '13 at 05:31