Re: Autolayout fixed size centering in VFL
Bringing this back up again ... Also, I suspect the reason you're specifying center-X alignment when creating the vertical constraints is because NSLayoutConstraint threw an exception when you tried to use the center-Y alignment option. That should be warning enough. ;-) Ignoring the larger problem for a minute, I don't think this specific alignment option is actually surprising. If you create a VERTICAL vfl string, the options value attribute tells the elements in the string how to line up. The string itself defines how the views relate to each other vertically - so it makes sense that the option value defines how the elements line up on the X axis. Specifically, with a string like this: @V:|-[titleLabel]-[descLabel]-[warningButton]-|; as part of [NSLayoutConstraint constraintsWithVisualFormat:VFLString options:NSLayoutFormatAlignAllLeft metrics:metrics views:views] the *NSLayoutFormatAlignAllLeft* option will line up each of the elements in the string (titleLabel, descLabel, warningButton) by their left edge. Given that 'warningButton' is the widest of the elements, If I add another constraint: @H:|-[warningButton]-| The constraints work together to both center the warningButton across the screen and then line up all the other elements according to the X axis option applied to the V: constraint. *Therefore, I now think that the original constraint we were talking about is correctly using the x-alignment when specifying a Vertical alignment.* So now, back to the original suggested code using VFL to center a view within its parent; I came across this post on objc.iohttp://www.objc.io/issue-3/advanced-auto-layout-toolbox.htmltoday which has the exact same code suggestion - and then proceeds to give a reasonable explanation as to why it works. If you visit the page, scroll down about 7/8 of the way through the article and you'll find it. This uses the option NSLayoutFormatAlignAllCenterX to create the actual centering constraint between the super view and the subview. The format string itself is merely a dummy that results in a constraint specifying that there should be less than one point of space between the super view’s bottom and the subview’s top edge, which is always the case as long as the subview is visible. You can reverse the dimensions in the example to achieve centering in the vertical direction. I'm still not sure how to rectify your superview / childview concern ... is there a formal condition on a VFL string that it cannot include elements at different levels in the view hierarchy - or more specifically, is it syntactically incorrect to include a parent and child view in that string? Curious if this behavior on iOS is expected then, or a bug. Thanks, -Luther On Fri, Oct 4, 2013 at 11:29 PM, Luther Baker lutherba...@gmail.com wrote: Also, I suspect the reason you're specifying center-X alignment when creating the vertical constraints is because NSLayoutConstraint threw an exception when you tried to use the center-Y alignment option. That should be warning enough. ;-) This is true - as I typed this block into my own project I naturally lined up the V: vfl string with the Y constant and the H: vfl string with the X constant ... and it complained. I then noticed that evgenyneuhttps://github.com/evgenyneu/center-vfl had flipped them. Thanks - it now feels like this is an anomaly at best. -Luther On Fri, Oct 4, 2013 at 11:03 PM, Kyle Sluder k...@ksluder.com wrote: On Fri, Oct 4, 2013, at 08:22 PM, Luther Baker wrote: First of all ... very much appreciate you both so often! Thanks for commenting as much as you do. I too faced this issue and, like Kyle, read enough to assume it was generally not possible with VFL. But this afternoon, I came across this: https://github.com/evgenyneu/center-vfl and for my specific case, it works perfectly. Let me know what you think ... and if you see caveats. I'm successfully using this technique to center an ImageView populated with an image much larger than the iPhone's screen on a simple, standard plain old ViewController's view. This doesn't work on OS X, and I'm surprised it works on iOS. UIView *superview = self.view; NSDictionary *variables = NSDictionaryOfVariableBindings(label, superview); NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@V:[superview]-(=1)-[label] options: NSLayoutFormatAlignAllCenterX metrics:nil views:variables]; [self.view addConstraints:constraints]; This specifies a relationship between the superview's bottom edge and the label's top edge, yet the label is a subview of the superview. When running on the Mac, I would expect
Re: Autolayout fixed size centering in VFL
On Fri, Oct 4, 2013, at 01:31 PM, jonat...@mugginsoft.com wrote: I have a fixed size custom OS X view that I load from a nib and want to centre within a host view using auto layout. Can this be done using VFL alone? No. But it's still really simple to do in code. To achieve the desired effect: a: The subview needs to acquire separate width and height constraints that reference itself. The width and height of the view are independent of its position. Correctly specifying the view's size is a separate issue. Typically that is done by virtue of the constraints installed _within_ the view. I personally cannot get a VFL only solution to work on OS X. Correct; you will not be able to. Notes: 1. I can achieve it simply in IB. Yup. 2. I can achieve it using explicit constraints like so: - (void)addCenteredSubview:(NSView *)subview { // if translatesAutoresizingMaskIntoConstraints = YES then constraints will be automatically added // when the view is added to a supview. we require to constrain manually set make sure the // translation is off. subview.translatesAutoresizingMaskIntoConstraints = NO; // with the above off we will need to apply width + height contstraints CGFloat width = subview.frame.size.width; CGFloat height = subview.frame.size.height; Again, why are you concerning yourself with the view's size here? It is not necessary to explicitly specify a size in order to get centering behavior. --Kyle Sluder ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Autolayout fixed size centering in VFL
On Fri, Oct 4, 2013, at 01:52 PM, Kyle Sluder wrote: The width and height of the view are independent of its position. Correctly specifying the view's size is a separate issue. Typically that is done by virtue of the constraints installed _within_ the view. (In other words, split up the centering constraints from the fixed-size constraints.) --Kyle Sluder ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Autolayout fixed size centering in VFL
On 4 Oct 2013, at 21:52, Kyle Sluder k...@ksluder.com wrote: On Fri, Oct 4, 2013, at 01:31 PM, jonat...@mugginsoft.com wrote: I have a fixed size custom OS X view that I load from a nib and want to centre within a host view using auto layout. Can this be done using VFL alone? No. Thanks for the confirmation. But it's still really simple to do in code. Simple and verbose. The VFL would be more concise. In reality I am just trying to figure out auto layout. Again, why are you concerning yourself with the view's size here? It is not necessary to explicitly specify a size in order to get centering behavior. I do see it as a necessity, given the approach listed. If a view provides intrinsic size info then explicit size constrains will not be required. A raw NSView instance requires the width/attributes to display correctly as far as my experiments have confirmed. This agrees with dumping layout constraints added in IB. In the following case auto layout fails because there are no size constraints. self.customview.translatesAutoresizingMaskIntoConstraints = NO; // we don't want any auto constraints applied [self.window.contentView addSubview: self.customview]; // recreate the fixed size centering constraints explictly /* [self.customview addConstraint:[NSLayoutConstraint constraintWithItem:self.customview attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:width]]; [self.customview addConstraint:[NSLayoutConstraint constraintWithItem:self.customview attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:height]]; */ [self.window.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.customview attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.window.contentView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]]; [self.window.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.customview attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.window.contentView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]]; Jonathan ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Autolayout fixed size centering in VFL
First of all ... very much appreciate you both so often! Thanks for commenting as much as you do. I too faced this issue and, like Kyle, read enough to assume it was generally not possible with VFL. But this afternoon, I came across this: https://github.com/evgenyneu/center-vfl and for my specific case, it works perfectly. Let me know what you think ... and if you see caveats. I'm successfully using this technique to center an ImageView populated with an image much larger than the iPhone's screen on a simple, standard plain old ViewController's view. (And yes, I know that the example code on that page' readme is iOS ... and this was a Mac OSX question - so I've not tested it explicitly). Thanks, -Luther --- UIView *superview = self.view;NSDictionary *variables = NSDictionaryOfVariableBindings(label, superview);NSArray *constraints =[NSLayoutConstraint constraintsWithVisualFormat:@V:[superview]-(=1)-[label] options: NSLayoutFormatAlignAllCenterX metrics:nil views:variables];[self.view addConstraints:constraints]; constraints =[NSLayoutConstraint constraintsWithVisualFormat:@H:[superview]-(=1)-[label] options: NSLayoutFormatAlignAllCenterY metrics:nil views:variables];[self.view addConstraints:constraints]; On Fri, Oct 4, 2013 at 6:56 PM, jonat...@mugginsoft.com jonat...@mugginsoft.com wrote: On 4 Oct 2013, at 21:52, Kyle Sluder k...@ksluder.com wrote: On Fri, Oct 4, 2013, at 01:31 PM, jonat...@mugginsoft.com wrote: I have a fixed size custom OS X view that I load from a nib and want to centre within a host view using auto layout. Can this be done using VFL alone? No. Thanks for the confirmation. But it's still really simple to do in code. Simple and verbose. The VFL would be more concise. In reality I am just trying to figure out auto layout. Again, why are you concerning yourself with the view's size here? It is not necessary to explicitly specify a size in order to get centering behavior. I do see it as a necessity, given the approach listed. If a view provides intrinsic size info then explicit size constrains will not be required. A raw NSView instance requires the width/attributes to display correctly as far as my experiments have confirmed. This agrees with dumping layout constraints added in IB. In the following case auto layout fails because there are no size constraints. self.customview.translatesAutoresizingMaskIntoConstraints = NO; // we don't want any auto constraints applied [self.window.contentView addSubview: self.customview]; // recreate the fixed size centering constraints explictly /* [self.customview addConstraint:[NSLayoutConstraint constraintWithItem:self.customview attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:width]]; [self.customview addConstraint:[NSLayoutConstraint constraintWithItem:self.customview attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:height]]; */ [self.window.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.customview attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.window.contentView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]]; [self.window.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.customview attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.window.contentView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]]; Jonathan ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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
Re: Autolayout fixed size centering in VFL
Sorry, I just noticed the link in the original question that contains the answer I was referring to. Now I'm curious to know if that doesn't work on the desktop or if you need to describe more than those simple two lines for other types of content/child views. I'm sure the UIImageView I am using can determine its own intrinsic size. -Luther On Fri, Oct 4, 2013 at 10:22 PM, Luther Baker lutherba...@gmail.com wrote: First of all ... very much appreciate you both so often! Thanks for commenting as much as you do. I too faced this issue and, like Kyle, read enough to assume it was generally not possible with VFL. But this afternoon, I came across this: https://github.com/evgenyneu/center-vfl and for my specific case, it works perfectly. Let me know what you think ... and if you see caveats. I'm successfully using this technique to center an ImageView populated with an image much larger than the iPhone's screen on a simple, standard plain old ViewController's view. (And yes, I know that the example code on that page' readme is iOS ... and this was a Mac OSX question - so I've not tested it explicitly). Thanks, -Luther --- UIView *superview = self.view;NSDictionary *variables = NSDictionaryOfVariableBindings(label, superview);NSArray *constraints =[NSLayoutConstraint constraintsWithVisualFormat:@V:[superview]-(=1)-[label] options: NSLayoutFormatAlignAllCenterX metrics:nil views:variables];[self.view addConstraints:constraints]; constraints =[NSLayoutConstraint constraintsWithVisualFormat:@H:[superview]-(=1)-[label] options: NSLayoutFormatAlignAllCenterY metrics:nil views:variables];[self.view addConstraints:constraints]; On Fri, Oct 4, 2013 at 6:56 PM, jonat...@mugginsoft.com jonat...@mugginsoft.com wrote: On 4 Oct 2013, at 21:52, Kyle Sluder k...@ksluder.com wrote: On Fri, Oct 4, 2013, at 01:31 PM, jonat...@mugginsoft.com wrote: I have a fixed size custom OS X view that I load from a nib and want to centre within a host view using auto layout. Can this be done using VFL alone? No. Thanks for the confirmation. But it's still really simple to do in code. Simple and verbose. The VFL would be more concise. In reality I am just trying to figure out auto layout. Again, why are you concerning yourself with the view's size here? It is not necessary to explicitly specify a size in order to get centering behavior. I do see it as a necessity, given the approach listed. If a view provides intrinsic size info then explicit size constrains will not be required. A raw NSView instance requires the width/attributes to display correctly as far as my experiments have confirmed. This agrees with dumping layout constraints added in IB. In the following case auto layout fails because there are no size constraints. self.customview.translatesAutoresizingMaskIntoConstraints = NO; // we don't want any auto constraints applied [self.window.contentView addSubview: self.customview]; // recreate the fixed size centering constraints explictly /* [self.customview addConstraint:[NSLayoutConstraint constraintWithItem:self.customview attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:width]]; [self.customview addConstraint:[NSLayoutConstraint constraintWithItem:self.customview attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:height]]; */ [self.window.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.customview attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.window.contentView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]]; [self.window.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.customview attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.window.contentView attribute:NSLayoutAttributeCenterY
Re: Autolayout fixed size centering in VFL
On Fri, Oct 4, 2013, at 08:22 PM, Luther Baker wrote: First of all ... very much appreciate you both so often! Thanks for commenting as much as you do. I too faced this issue and, like Kyle, read enough to assume it was generally not possible with VFL. But this afternoon, I came across this: https://github.com/evgenyneu/center-vfl and for my specific case, it works perfectly. Let me know what you think ... and if you see caveats. I'm successfully using this technique to center an ImageView populated with an image much larger than the iPhone's screen on a simple, standard plain old ViewController's view. This doesn't work on OS X, and I'm surprised it works on iOS. UIView *superview = self.view; NSDictionary *variables = NSDictionaryOfVariableBindings(label, superview); NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@V:[superview]-(=1)-[label] options: NSLayoutFormatAlignAllCenterX metrics:nil views:variables]; [self.view addConstraints:constraints]; This specifies a relationship between the superview's bottom edge and the label's top edge, yet the label is a subview of the superview. When running on the Mac, I would expect NSLayoutConstraint to complain here , but I can see why it might not if the |- syntax is treated as a simple shortcut for whatever the superview happens to be. Indeed, if you try the equivalent on the Mac, you can see that this code throws an exception because it tries to install a constraint relating a view to another view upon that view itself: Unable to install constraint on view. Does the constraint reference something from outside the subtree of the view? That's illegal. constraint:NSLayoutConstraint:0x104406a00 V:[NSView:0x10060da80]-(=1)-[NSTextField:0x10012ac00] view:NSView: 0x10060da80 I've forked the project you linked to and uploaded my demo: https://github.com/kylesluder/center-vfl/ Also, I suspect the reason you're specifying center-X alignment when creating the vertical constraints is because NSLayoutConstraint threw an exception when you tried to use the center-Y alignment option. That should be warning enough. ;-) --Kyle Sluder ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Autolayout fixed size centering in VFL
Also, I suspect the reason you're specifying center-X alignment when creating the vertical constraints is because NSLayoutConstraint threw an exception when you tried to use the center-Y alignment option. That should be warning enough. ;-) This is true - as I typed this block into my own project I naturally lined up the V: vfl string with the Y constant and the H: vfl string with the X constant ... and it complained. I then noticed that evgenyneuhttps://github.com/evgenyneu/center-vfl had flipped them. Thanks - it now feels like this is an anomaly at best. -Luther On Fri, Oct 4, 2013 at 11:03 PM, Kyle Sluder k...@ksluder.com wrote: On Fri, Oct 4, 2013, at 08:22 PM, Luther Baker wrote: First of all ... very much appreciate you both so often! Thanks for commenting as much as you do. I too faced this issue and, like Kyle, read enough to assume it was generally not possible with VFL. But this afternoon, I came across this: https://github.com/evgenyneu/center-vfl and for my specific case, it works perfectly. Let me know what you think ... and if you see caveats. I'm successfully using this technique to center an ImageView populated with an image much larger than the iPhone's screen on a simple, standard plain old ViewController's view. This doesn't work on OS X, and I'm surprised it works on iOS. UIView *superview = self.view; NSDictionary *variables = NSDictionaryOfVariableBindings(label, superview); NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@V:[superview]-(=1)-[label] options: NSLayoutFormatAlignAllCenterX metrics:nil views:variables]; [self.view addConstraints:constraints]; This specifies a relationship between the superview's bottom edge and the label's top edge, yet the label is a subview of the superview. When running on the Mac, I would expect NSLayoutConstraint to complain here , but I can see why it might not if the |- syntax is treated as a simple shortcut for whatever the superview happens to be. Indeed, if you try the equivalent on the Mac, you can see that this code throws an exception because it tries to install a constraint relating a view to another view upon that view itself: Unable to install constraint on view. Does the constraint reference something from outside the subtree of the view? That's illegal. constraint:NSLayoutConstraint:0x104406a00 V:[NSView:0x10060da80]-(=1)-[NSTextField:0x10012ac00] view:NSView: 0x10060da80 I've forked the project you linked to and uploaded my demo: https://github.com/kylesluder/center-vfl/ Also, I suspect the reason you're specifying center-X alignment when creating the vertical constraints is because NSLayoutConstraint threw an exception when you tried to use the center-Y alignment option. That should be warning enough. ;-) --Kyle Sluder ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com