• 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
Autolayout fixed size centering in VFL
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Autolayout fixed size centering in VFL


  • Subject: Autolayout fixed size centering in VFL
  • From: email@hidden
  • Date: Fri, 04 Oct 2013 21:31:29 +0100

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?

My best shot at it follows, but it is incorrect:

    [self.window.contentView addConstraints:[NSLayoutConstraint
                                             constraintsWithVisualFormat:@"|-(>=0)-[subview(width)]-(>=0)-|"
                                             options:NSLayoutFormatAlignAllCenterY
                                             metrics:@{@"width": @(width)}
                                             views:@{@"subview": self.subview}]];

    [self.window.contentView addConstraints:[NSLayoutConstraint
                                             constraintsWithVisualFormat:@"V:|-(>=0)-[subview(height)]-(>=0)-|"
                                             options:NSLayoutFormatAlignAllCenterX
                                             metrics:@{@"height": @(height)}
                                             views:@{@"subview": self.subview}]];

To achieve the desired effect:

a: The subview needs to acquire separate width and height constraints that reference itself.
b: The contentView needs to acquire constraints that center the subview within it.

The autolayout docs say:

    The notation prefers good visualization over completeness of expressibility.
    There are constraints that cannot be expressed in visual format syntax, although most of the constraints that are useful in real user interfaces can be.

I would have intuitively thought that this was doable.
Others (http://stackoverflow.com/questions/12873372/centering-a-view-in-its-superview-using-visual-format-language) seem divided on the question.
I personally cannot get a VFL only solution to work on OS X.

Notes:

1. I can achieve it simply in IB.
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;


    // add width and height constraints to the subview
    [subview addConstraint:[NSLayoutConstraint
                                    constraintWithItem:subview
                                    attribute:NSLayoutAttributeWidth
                                    relatedBy:NSLayoutRelationEqual
                                    toItem:nil
                                    attribute:NSLayoutAttributeNotAnAttribute
                                    multiplier:1.0
                                    constant:width]];

    [subview addConstraint:[NSLayoutConstraint
                                    constraintWithItem:subview
                                    attribute:NSLayoutAttributeHeight
                                    relatedBy:NSLayoutRelationEqual
                                    toItem:nil
                                    attribute:NSLayoutAttributeNotAnAttribute
                                    multiplier:1.0
                                    constant:height]];

    // the subview needs to be part of the view hierarchy before constraints
    // can be applied that relate the superview and the subview
    [self addSubview:subview];

    [self addConstraint:[NSLayoutConstraint
                                            constraintWithItem:subview
                                            attribute:NSLayoutAttributeCenterX
                                            relatedBy:NSLayoutRelationEqual
                                            toItem:self
                                            attribute:NSLayoutAttributeCenterX
                                            multiplier:1.0
                                            constant:0]];

    [self addConstraint:[NSLayoutConstraint
                                            constraintWithItem:subview
                                            attribute:NSLayoutAttributeCenterY
                                            relatedBy:NSLayoutRelationEqual
                                            toItem:self
                                            attribute:NSLayoutAttributeCenterY
                                            multiplier:1.0
                                            constant:0]];
}









_______________________________________________

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: Kyle Sluder <email@hidden>
  • Prev by Date: Re: why isn't id<MyCellDelegate> an id?
  • Next by Date: Re: Autolayout fixed size centering in VFL
  • Previous by thread: Re: How to get variably sized header in a UICollectionView supporting both orientations
  • Next by thread: Re: Autolayout fixed size centering in VFL
  • Index(es):
    • Date
    • Thread