本文介绍了使用Visual格式语言中心的视图,它的父的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我刚开始学习自动版式iOS和看了一眼视觉格式语言。

I just started learning AutoLayout for iOS and had a look at Visual Format Language.

它所有的工作除了一件事罚款:我只是不能为了它的父上居中结果
这可能与VFL还是需要手动创建一个约束自己?

It all works fine except for one thing: I just can't get a view to center within its superview.
Is this possible with VFL or do I need to manually create a constraint myself?

推荐答案

目前,不,它并不像它可以利用中心在上海华视图的只有的VFL。它是,但是,并不难,使用单个VFL字符串和一个额外的约束(每轴)做到这一点:

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) - [查看] - (大于= 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}];

我试过上面试图把它屈从于我的意志的许多不同的变化,但这似乎并不适用于上海华甚至明确地有两个独立的VFL字符串时,两轴( H:|五:)。然后,我开始尝试,准确地隔离时的选项不要的得到应用到VFL。他们似乎的的适用于VFL的上海华和只适用于那些在VFL字符串(这是令人失望的在某些情况下)提到任何明确的意见。

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).

我希望在未来的苹果增加了一些新的选项,使VFL选项考虑到上海华,即使这样做只有当有只除了在VFL的上海华独显视图。另一种解决方案可能是传递到VFL另一种选择,说是这样的: NSLayoutFormatOptionIncludeSuperview

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.

不用说,我学到了很多关于VFL试图回答这个问题。

Needless to say, I learned a lot about VFL trying to answer this question.

这篇关于使用Visual格式语言中心的视图,它的父的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 14:21