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 (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