将NSLayoutConstraints应用于UIView子类

5

我之前尝试过在IB中使用自动布局约束,并且理解了它们的工作原理以及需要避免歧义等问题。

但是当你有多个视图时,情况会变得有些复杂,往往会出现问题。

所以,我刚刚读到一个关于一个人遇到同样问题并使用ASCII代码创建约束的博客。

我有一个UITableView,其中包含一个根据内容量动态调整大小的自定义UITableViewCell。这是自动布局的完美对象。

因此,我尝试了几种方法,现在我缩小了内容,只在单元格中放置了一个标签。

我想让标签填充整个单元格,并围绕其添加边框。

也就是说,标准大小点从每个单元格的边缘内部计算。

我是这样做的...

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
        _label.textColor = [UIColor colorWithWhite:0.53 alpha:1.0];
        _label.backgroundColor = [UIColor yellowColor];
        [self addSubview:_label];

        NSDictionary *views = NSDictionaryOfVariableBindings(_label);

        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[_label]-|"
                                                                     options:0
                                                                     metrics:nil
                                                                       views:views]];

        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-[_label]-|"
                                                                     options:0
                                                                     metrics:nil
                                                                       views:views]];

        self.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

但是当单元格被显示时,标签位于左上角,与我用来初始化它的CGRect完全相同,并且会出现关于约束的许多错误...

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:0x1f859e40 h=--& v=--& UILabel:0x1f864a00.midX == + 5>",
    "<NSLayoutConstraint:0x1f86a4f0 H:|-(NSSpace(20))-[UILabel:0x1f864a00]   (Names: '|':MyCell:0x1f857740 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x1f86a4f0 H:|-(NSSpace(20))-[UILabel:0x1f864a00]   (Names: '|':MyCell:0x1f857740 )>

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2013-03-07 11:56:14.841 unasys[13082:907] 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) 
(
    "<NSLayoutConstraint:0x1f85f7b0 V:|-(NSSpace(20))-[UILabel:0x1f864a00]   (Names: '|':MyCell:0x1f857740 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x1f859ec0 h=--& v=--& UILabel:0x1f864a00.midY == + 5>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x1f85f7b0 V:|-(NSSpace(20))-[UILabel:0x1f864a00]   (Names: '|':MyCell:0x1f857740 )>

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2013-03-07 11:56:14.854 unasys[13082:907] 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) 
(
    "<NSLayoutConstraint:0x1f86a540 H:[UILabel:0x1f864a00]-(NSSpace(20))-|   (Names: '|':MyCell:0x1f857740 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x1f859e40 h=--& v=--& UILabel:0x1f864a00.midX == + 5>",
    "<NSAutoresizingMaskLayoutConstraint:0x1f859e80 h=--& v=--& H:[UILabel:0x1f864a00(10)]>",
    "<NSAutoresizingMaskLayoutConstraint:0x1ed8e150 h=--& v=--& H:[MyCell:0x1f857740(320)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x1f86a540 H:[UILabel:0x1f864a00]-(NSSpace(20))-|   (Names: '|':MyCell:0x1f857740 )>

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2013-03-07 11:56:14.858 unasys[13082:907] 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:0x1eda7260 h=--& v=--& V:[MyCell:0x1f857740(143)]>",
    "<NSAutoresizingMaskLayoutConstraint:0x1f859f00 h=--& v=--& V:[UILabel:0x1f864a00(10)]>",
    "<NSLayoutConstraint:0x1f85f870 V:[UILabel:0x1f864a00]-(NSSpace(20))-|   (Names: '|':MyCell:0x1f857740 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x1f859ec0 h=--& v=--& UILabel:0x1f864a00.midY == + 5>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x1f85f870 V:[UILabel:0x1f864a00]-(NSSpace(20))-|   (Names: '|':MyCell:0x1f857740 )>

我不知道从哪里开始调试它。虽然约束条件非常简单,但却完全失败了。

有人能告诉我如何让这个约束条件生效吗?

1个回答

13

在这一行之后

_label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];

添加

[_label setTranslatesAutoresizingMaskIntoConstraints:NO];

默认情况下,自动调整大小掩码也会转换为约束条件,这与您设置的约束条件冲突,使其不明确。

文档在这里:setTranslatesAutoresizingMaskIntoConstraints


可以了!我还得把添加的东西改为self.contentView,而不是self,因为它是UITableViewCell,然后一切都运作正常了:D 谢谢! - Fogmeister
1
基本上,如果您在自动布局错误中看到 NSAutoresizingMaskLayoutConstraint,并且不知道它为什么存在,那么您可能忘记了在某个地方关闭 translatesAutoresizingMaskIntoConstraints - iain
非常感谢,我会记住下次的。感谢您的帮助。 - Fogmeister

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接