4
votes

I have an NSImageView in a NSView set up in IB. The NSImageView is exactly the same size as the NSView.

Everything works fine and the NSImageView have the same size as the NSView when resizing the window.

BUT, now I've added an animation (move from A to B) to the NSImageView and that will mess up the constraints that's been set up in IB. So I have to do this programmatically.

How could I programmatically set NSLayoutConstraint to have my NSImageView have the same size as my NSImageView (the superview)?

UPDATE:

Just to give you guys some more information. My app uses a split view (three views) and instead of adding the NSImageView in IB I now add it programmatically. I add my new view (which shall be scalable to it's parent view) to the third view in the split view.

Do you think the split view is causing these issues?

UPDATE 2:

Ok, I'm closer to fixing this. I removed the NSImageView from the view, and added a NSView instead. The NSView scaled fine, but as soon as I added the NSImageView it stopped working in the way that the NSImageView won't scale after resizing the window.

In other words, the problem lies in the NSImageView itself. It won't scale after resizing the window...

SOLVED

I solved it by using PDF View instead of image view (it was a PDF I wanted to show). I set the autoScales property to YES on the PDF View.

1
With the constraints as given by Fruity Geek your program should work.Are you sure there are no other constraints added automagically by IB?kwicher
Strange... I've updated added some information to my question. Still don't get it to work.Mikael

1 Answers

4
votes

I assume you already have references to the views in the example below:

NSView * parentView;
NSImageView * imageView;
[imageView setTranslatesAutoresizingMaskIntoConstraints:NO]; //Required to opt-in to autolayout
[parentView addSubview:imageView]; //Subview must exist before adding constraint. 

NSDictionary * views = NSDictionaryOfVariableBindings(imageView);
[parentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[imageView]|"
                                                                   options:0
                                                                   metrics:nil
                                                                     views:views]];
[parentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[imageView]|"
                                                                   options:0
                                                                   metrics:nil
                                                                     views:views]];