Re: Autolayout fixed size centering in VFL
Re: Autolayout fixed size centering in VFL
- Subject: Re: Autolayout fixed size centering in VFL
- From: Luther Baker <email@hidden>
- Date: Mon, 07 Oct 2013 16:03:32 -0500
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.io<http://www.objc.io/issue-3/advanced-auto-layout-toolbox.html>today
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 <email@hidden> 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 evgenyneu<https://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 <email@hidden> 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 (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