Re: 10.7 Auto Layout re-sizing question
Re: 10.7 Auto Layout re-sizing question
- Subject: Re: 10.7 Auto Layout re-sizing question
- From: Kevin Cathey <email@hidden>
- Date: Thu, 23 Feb 2012 09:13:11 -0800
Jonathan,
> viewController = [[RemindersViewController alloc] initWithNibName:@"RemindersViewController" bundle:nil];
> self.window.contentView = viewController.view;
> [[viewController view] setFrame: [myView bounds]];
Did you mean do make the content view of the window the view controller's view? Or did you mean to call addSubview instead on line 2?
In order for the loaded subview to correctly fill it's parent, you need to add constraints in code. Here's what I would do:
Load the view via some nib loading mechanism (preferably a view controller)
Ensure the view has translatesAutoresizingMaskIntoConstraints set to NO
When translatesAutoresizingMaskIntoConstraints is YES, the autoresizing is used to generate additional constraints. This can often cause conflicts if you're not expecting this, and this is a property intended only for transitioning applications. You want to turn this off so you can specify the position/size using constraints only.
By default, Xcode will set the value of translatesAutoresizingMaskIntoConstraints to YES for top level views, including those loaded for a view controller. The reason for this is also transitional: right now AppKit view subclasses usually expect subviews with translatesAutoresizingMaskIntoConstraints set to YES, and most applications are transitioning. Maybe some day in the future the default will be changed to NO.
You can set the value of translatesAutoresizingMaskIntoConstraints in code, or if you are using Xcode 4.3, you can use the View Attributes inspector to uncheck the value so translatesAutoresizingMaskIntoConstraints will be loaded from the nib with a value of NO.
Add as a subview
Add constraints
Using Mail as a compiler, here's the code snippet I would use:
// Load the nib
viewController = [[RemindersViewController alloc] initWithNibName:@"RemindersViewController" bundle:nil];
NSView *mySubview = [viewController view];
// Update the value of translatesAutoresizingMaskIntoConstraints if needed (or with Xcode 4.3+, do this in the Attributes Inspector in the NIB)
[mySubview setTranslatesAutoresizingMaskIntoConstraints:NO];
// Add as a subview
[[[self window] contentView] addSubview:mySubview];
// Add constraints constraining the subview to it's superview's edges
[[[self window] contentView] addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[mySubview]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(mySubview)]];
[[[self window] contentView] addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[mySubview]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(mySubview)]];
// You cannot call setFrame anymore, since the value of the frame will just get stomped when the next layout pass happens.
//[[viewController view] setFrame: [myView bounds]];
Kevin
On 23 Feb 2012, at 06:11, Jonathan Waddilove <email@hidden> wrote:
> In the past I have managed to add a NSViewController's view to a subView of my window controller, usually something like
>
> viewController = [[RemindersViewController alloc] initWithNibName:@"RemindersViewController" bundle:nil];
> self.window.contentView = viewController.view;
> [[viewController view] setFrame: [myView bounds]];
>
> Now I'm trying to get with the program and use auto layouts. Both NIBs (window & view controller) have auto layout enabled and the contents of the VC view (a table) should re-size to fill the view.
>
> Except (of course) the re-sizing doesn't work. I'm obviously missing something very simple. Please could some kind person explain how we make the dynamically added subview correctly track into the parent views 'custom view'
>
> Many thanks, Jonathan
>
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden