Ios 自动布局:带填充的UIView内的UILabel

Ios 自动布局:带填充的UIView内的UILabel,ios,autolayout,uilabel,Ios,Autolayout,Uilabel,我试图掌握自动布局,但我很难看到如何实现一些过去很简单的东西。我有一个具有内在大小的UILabel*标签,我想把它放在UIView*容器中。标签应有20个点的填充,以便在容器内垂直和水平居中 有几个类似的问题指向重写intrinsicContentSize方法,但这不适用于具有多行文本的标签,需要我对标签进行子类化 我尝试了以下方法: [self addConstraints:[NSLayoutConstraint constraintsWithVis

我试图掌握自动布局,但我很难看到如何实现一些过去很简单的东西。我有一个具有内在大小的
UILabel*标签
,我想把它放在
UIView*容器中
。标签应有20个点的填充,以便在容器内垂直和水平居中

有几个类似的问题指向重写
intrinsicContentSize
方法,但这不适用于具有多行文本的标签,需要我对标签进行子类化

我尝试了以下方法:

[self addConstraints:[NSLayoutConstraint
                      constraintsWithVisualFormat:@"H:|-20-[label]-20-|"
                      options:0
                      metrics:nil
                      views:NSDictionaryOfVariableBindings(label)]];

[self addConstraints:[NSLayoutConstraint
                      constraintsWithVisualFormat:@"V:|-20-[label]-20-|"
                      options:0
                      metrics:nil
                      views:NSDictionaryOfVariableBindings(label)]];
这确实为容器提供了正确的大小,但在标签仍位于左上角的情况下发出警告

有人能告诉我哪里出了问题吗?我肯定我还没有弄明白,但是对于这样一个简单的任务来说,自动布局似乎相当复杂

更新:以下是我收到的2个错误中的1个(第二个是相同的,但用于垂直约束)

2015-10-08 13:36:58.869 Thinger[17498:8116452]无法同时满足约束。
可能下面列表中至少有一个约束是您不想要的。试着这样做:(1)看看每个约束,试着找出你不期望的约束;(2) 找到添加了不需要的约束的代码,然后修复它。(注意:如果您看到不理解的NSAutoresizingMaskLayoutConstraints,请参阅UIView属性TranslatesAutoResizingMaskToConstraints的文档)
(
"",
"",
""
)
将尝试通过打破约束进行恢复
在UIViewAlertForUnsatifiableConstraints处创建一个符号断点,以便在调试器中捕获该断点。
中列出的UIView上UIConstraintBasedLayoutDebugging类别中的方法也可能会有所帮助。

与USCA共享警告消息。您可以告诉我们您收到的警告是什么。@baydi-我刚刚将标签上的
自动调整大小GMaskintoConstraints
设置为
。这是正确的方法吗?感谢您的回复。您不应该提供前导和尾随约束以使其居中。尝试给出centreX和centry约束。
2015-10-08 13:36:58.869 Thinger[17498:8116452] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x7c23c480 h=--& v=--& UILabel:0x7c140b80'Hello world'.midX == + 38.5>",
    "<NSAutoresizingMaskLayoutConstraint:0x7c2bb500 h=--& v=--& H:[UILabel:0x7c140b80'Hello world'(77)]>",
    "<NSLayoutConstraint:0x7c144b10 H:|-(20)-[UILabel:0x7c140b80'Hello world']   (Names: '|':BigButtonView:0x7c142460 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7c144b10 H:|-(20)-[UILabel:0x7c140b80'Hello world']   (Names: '|':BigButtonView:0x7c142460 )>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.