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

Reply via email to