gpt4 book ai didi

iphone - 将 NSLayoutConstraints 应用于 UIView 子类

转载 作者:可可西里 更新时间:2023-11-01 04:16:17 24 4
gpt4 key购买 nike

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

但是当您有多个 View 时,它会变得有点复杂并且容易崩溃。

因此,我刚刚阅读了一篇博客,其中介绍了一个人遇到了同样的问题并使用 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 )>

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

有人能为我指出正确的方向,让我知道如何让这个约束起作用吗?

最佳答案

行后

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

添加

[_label setTranslatesAutoresizingMaskIntoConstraints:NO];

默认情况下autoresizingmask也变成了constraints,和你设置的constraints有冲突,造成歧义

文档在这里:setTranslatesAutoresizingMaskIntoConstraints

关于iphone - 将 NSLayoutConstraints 应用于 UIView 子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15270883/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com