• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Autolayout fixed size centering in VFL
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Autolayout fixed size centering in VFL


  • Subject: Re: Autolayout fixed size centering in VFL
  • From: "email@hidden" <email@hidden>
  • Date: Sat, 05 Oct 2013 00:56:05 +0100

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


  • Follow-Ups:
    • Re: Autolayout fixed size centering in VFL
      • From: Luther Baker <email@hidden>
References: 
 >Autolayout fixed size centering in VFL (From: email@hidden)
 >Re: Autolayout fixed size centering in VFL (From: Kyle Sluder <email@hidden>)

  • Prev by Date: Re: C++ Function Call from Obj-C
  • Next by Date: Re: How to get variably sized header in a UICollectionView supporting both orientations
  • Previous by thread: Re: Autolayout fixed size centering in VFL
  • Next by thread: Re: Autolayout fixed size centering in VFL
  • Index(es):
    • Date
    • Thread