【问题标题】:Applying NSLayoutConstraints to a UIView subclass将 NSLayoutConstraints 应用于 UIView 子类
【发布时间】:2013-03-07 12:09:16
【问题描述】:

我之前在 IB 中尝试过使用 AutoLayout 约束,我了解它们的工作原理以及它们需要明确等...

但是当你有多个视图时,它会变得有点复杂并且容易中断。

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

我有一个带有自定义 UITableViewCell 的 UITableView,它具有基于内容量的动态大小。自动布局的完美候选者。

所以,我尝试了几种方法,现在我已经减少了内容,以便单元格中只有一个标签。

如果标签用边框填充单元格,我想要什么。

即标准尺寸点从单元格的每个边缘开始。

我已经这样做了......

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

我不知道从哪里开始调试它。约束是绝对最小的,但仍然完全失败。

有人能指出如何让这个约束发挥作用的正确方向吗?

【问题讨论】:

    标签: iphone ios autolayout nslayoutconstraint


    【解决方案1】:

    行后

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

    添加

    [_label setTranslatesAutoresizingMaskIntoConstraints:NO];
    

    默认情况下,自动调整掩码也会变成约束,这与您设置的约束冲突,使其模棱两可

    文档在这里:setTranslatesAutoresizingMaskIntoConstraints

    【讨论】:

    • 太棒了!非常感谢!
    • 有效!我还必须更改以将内容添加到 self.contentView 而不是 self 因为它是 UITableViewCell 然后一切正常:D 谢谢!
    • 基本上,如果您在自动布局错误中看到 NSAutoresizingMaskLayoutConstraint 并且您不知道它为什么在那里,那么您忘记在某处关闭 translatesAutoresizingMaskIntoConstraints
    • 非常感谢,下次我会记住的。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-26
    • 2018-02-17
    • 2012-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多