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

Reply via email to