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: Fri, 04 Oct 2013 22:22:21 -0500
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, email@hidden <
email@hidden> wrote:
> On 4 Oct 2013, at 21:52, Kyle Sluder <email@hidden> wrote:
>
> > On Fri, Oct 4, 2013, at 01:31 PM, email@hidden 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 (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