I just started learning AutoLayout for iOS and had a look at Visual Format Language.
It all works fine except for one thing: I just can\'t get a view to center withi
@igor-muzyka answer in Swift 2:
NSLayoutConstraint.constraintsWithVisualFormat("H:[view]-(<=1)-[subview]",
options: .AlignAllCenterY,
metrics: nil,
views: ["view":view, "subview":subview])
Just need to say, that u can use this instead of constraints:
child.center = [parent convertPoint:parent.center fromView:parent.superview];
Currently, no, it doesn't look like it is possible to center a view in the superview using only VFL. It is, however, not that difficult to do it using a single VFL string and a single extra constraint (per axis):
VFL: "|-(>=20)-[view]-(>=20)-|"
[NSLayoutConstraint constraintWithItem:view
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:view.superview
attribute:NSLayoutAttributeCenterX
multiplier:1.f constant:0.f];
One would think that you would simply be able to do this (which is what I initially thought and tried when I saw this question):
[NSLayoutConstraint constraintsWithVisualFormat:@"|-(>=20)-[view(==200)]-(>=20)-|"
options: NSLayoutFormatAlignAllCenterX | NSLayoutFormatAlignAllCenterY
metrics:nil
views:@{@"view" : view}];
I tried many different variations of the above trying to bend it to my will, but this does not appear to apply to the superview even when explicitly having two separate VFL strings for both axes (H:|V:
). I then started to try and isolate exactly when the options do get applied to the VFL. They appear to not apply to the superview in the VFL and will only apply to any explicit views that are mentioned in the VFL string (which is disappointing in certain cases).
I hope in the future Apple adds some kind of new option to have the VFL options take into account the superview, even if doing it only when there is only a single explicit view besides the superview in the VFL. Another solution could be another option passed into the VFL that says something like: NSLayoutFormatOptionIncludeSuperview
.
Needless to say, I learned a lot about VFL trying to answer this question.
Thanks to the answer from @Evgenii, I create a full example in gist:
center the red square vertically with custom height
https://gist.github.com/yallenh/9fc2104b719742ae51384ed95dcaf626
You can center a view vertically by useing either VFL (which is not quite intuitive) or use constraints manually. By the way, I cannot combine both centerY and height together in VFL like:
"H:[subView]-(<=1)-[followIcon(==customHeight)]"
In the current solution VFL constraints are added separately.
What it have to be done is that superview should be declared in the dictionary. Instead of using |
you use @{@"super": view.superview};
.
And NSLayoutFormatAlignAllCenterX
for vertical and NSLayoutFormatAlignAllCenterY
for horizontal as options.
I know it's not want you want but you can of course calculate the margins and use them to create the visual format string ;)
Anyway, no. Unfortunatly it's not possible to do that 'automatically' with VFL - at least not yet.