5

I am having troubles getting my subviews to resize correctly using Autolayout. To illustrate my point, I put together a minimalistic example.

First I created a new NSViewController and added a subview to it (in this particular case an NSTextView) and added Autolayout constraints.

enter image description here

I then added a custom view to my MainMenu.xib and set up Autolayout constraints for that too.

enter image description here

Finally, I created an instance of my view controller and put its view inside my custom view.

#import "AppDelegate.h"
#import "MyViewController.h"

@interface AppDelegate()
@property (weak) IBOutlet NSView *customView;
@end

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    MyViewController *myViewController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];

    [self.customView addSubview:myViewController.view];
    myViewController.view.frame = self.customView.bounds;
}

@end

Since "Autoresizes Subviews" is checked in both xib files, I would expect the NSTextView to resize when I resize the main window. However, it just stays in place.

enter image description here

What am I missing here? This has puzzled me for a few days now.

Thanks, Michael Knudsen

2 Answers 2

8

Just in case anybody else runs into the same problem. I ended up solving it this way (thanks to @SevenBits for pointing me in that direction).

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    MyViewController *myViewController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];

    [self.customView addSubview:myViewController.view];

    myViewController.view.translatesAutoresizingMaskIntoConstraints = NO;

    NSArray *verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[subView]|"
                                                                           options:0
                                                                           metrics:nil
                                                                             views:@{@"subView" : myViewController.view}];

    NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[subView]|"
                                                                           options:0
                                                                           metrics:nil
                                                                             views:@{@"subView" : myViewController.view}];

    [self.customView addConstraints:verticalConstraints];
    [self.customView addConstraints:horizontalConstraints];
}
0
3

Your NSTextView's containing view does not have any constraints to its parent, in this case your window. The view that you added is not getting resized, because since your view is not "connected" via constraints to its parent. You might want to investigate the addConstraint: method if you want to do this programmatically.

See Apple's docs for more information.

5
  • If you look at the first screenshot in my post, it appears that the NSTextView (or more precisely the NSScrollView in which it is embedded) is a subview of the IBOutlet view of my view controller. The autolayout constraints are with respect to that view, right? Feb 26, 2014 at 13:19
  • I actually did experiment with doing it programmatically, but somehow that ended up preventing me from resizing anything at all. It simple wasn't possible to "grab onto" the window corners/sides after a view was added programmatically. Guess it's time to head back to the documentation and give it yet another try. Feb 26, 2014 at 13:34
  • Yes, the auto layout constraints are in respect to the view holding (I.e the parent view of) the text view.
    – SevenBits
    Feb 26, 2014 at 13:37
  • Hmmm. Then what do you mean when you say that the text view is not "connected" to its parent? Feb 26, 2014 at 13:42
  • Thanks. Makes a lot more sense now. I will try to experiment with that. Stay tuned :-) Feb 26, 2014 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.