Re: Autolayout fixed size centering in VFL
Re: Autolayout fixed size centering in VFL
- Subject: Re: Autolayout fixed size centering in VFL
- From: Kyle Sluder <email@hidden>
- Date: Fri, 04 Oct 2013 13:52:13 -0700
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. 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 (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