gpt4 book ai didi

ios - 在 UITableViewCell 中自动布局两个标签?

转载 作者:行者123 更新时间:2023-11-29 03:25:40 25 4
gpt4 key购买 nike

我是 iOS 编程的新手,可能不了解 View 层次结构,因此我无法在我创建的自定义表格单元格类中成功获取两个标签以正确调整大小。即 "translatesAutoresizingMaskIntoConstraints" 属性让我有点困惑。

我没有为这部分代码使用 Storyboard:我有一个 TableViewController,我在 viewDidLoad 中创建了自己的 tableView。在 cellForRowAtIndexPath 中,我初始化了自己的 TableViewCell 实现。

我遇到的问题是,当我将 TableView 和我创建的 UILabel 的 "setTranslatesAutoresizingMaskIntoConstraints" 设置为 NO 然后添加约束时,我得到了以下错误:

"Terminating app due to uncaught exception `'NSInternalInconsistencyException',` reason: 'Auto Layout still required after executing `-layoutSubviews`. UITableView's implementation of `-layoutSubviews` needs to call super.'"

如果我注释掉 setTranslatesAutoresizingMaskIntoConstraints 行,我的应用程序会运行,但我会收到以下有关约束的警告:

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

基本上我想做的是在此处输入代码,让两个标签相互齐平,并根据方向/设备调整它们的大小(我将在它们上设置背景颜色,因此希望它们看起来“连续” )

任何人都可以帮助我并解释我所缺少的吗?提前致谢。

我添加标签的代码是:

self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 30.0f)];
self.nameLabel.textColor = [UIColor redColor];
self.nameLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:12.0f];
self.nameLabel.backgroundColor = [UIColor brownColor];
[self.nameLabel setText:@"Test"];
// [self.nameLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.contentView addSubview:self.nameLabel];

...

NSDictionary *viewsDictionary = 
NSDictionaryOfVariableBindings(nameLabel, summaryLabel);
NSArray *constraints =
[NSLayoutConstraint constraintsWithVisualFormat:@"|-[nameLabel][summaryLabel]-|"
options:0
metrics:nil
views:viewsDictionary];

最佳答案

I'm fairly new to ios programming and probably don't understand the view hierarchy as well as I should and thus am failing to successfully get two labels within a custom table cell class I have created to autoresize properly. Namely the "setTranslatesAutoresizingMaskIntoConstraints" property has me a little confused.

嗯,translatesAutoresizingMaskIntoConstraints是 Apple 创建的用于从 Autoresizing (Spring and Struts) 过渡的属性至 Autolayout更轻松。说,你有一些 AutoresizingMasks为了您的观点,您刚刚切换了Autolayout在不设置任何约束的情况下打开。然后你现有的 AutoresizingMasks将转换为约束,将 View 固定在适当的位置。所以,默认情况下 translatesAutoresizingMaskIntoConstraints属性设置为 YES。但是,当您开始添加约束时,在 90% 的情况下,它们将与通过转换 AutoresizingMasks 创建的约束发生冲突。 .所以,最好通过设置 view.translatesAutoresizingMaskIntoConstraints = NO 来关闭它。

在您的代码中,以下问题可能是造成问题的原因:


  • Frame的设置

您不应该为要添加约束的对象设置框架。这是一种范式转变。当您以自动布局方式思考时,框架只是设置正确约束的结果,这些约束共同决定了相关 View 的框架。所以,请删除框架设置。

self.nameLabel = [[UILabel alloc] init];就足够了。

  • 设置适当的约束

您的代码:


NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(nameLabel, summaryLabel);
NSArray *constraints =
[NSLayoutConstraint constraintsWithVisualFormat:@"|-[nameLabel][summaryLabel]-|"
options:0
metrics:nil
views:viewsDictionary];
  • 名称标签

    • 现在,上述约束告诉我们 nameLabel水平(因为您没有提到 H: 或 V:)与容器间隔“标准”距离 (20px),与 summaryLabel 相邻.但是它的 Y 位置和宽度和高度呢?所以我们需要更多的约束。
  • summaryLabel

    • 同样适用于 summaryLabel .

所以,让我们正确定义它们:

NSDictionary *viewsDictionary =
NSDictionaryOfVariableBindings(nameLabel, summaryLabel);
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[nameLabel(100)][summaryLabel]-|" options:0 metrics:nil views:viewsDictionary];
NSArray *constraints1 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[nameLabel(30)]" options:0 metrics:nil views:viewsDictionary];
NSArray *constraints2 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[summaryLabel]" options:0 metrics:nil views:viewsDictionary];
NSArray *constraints3 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[nameLabel(==summaryLabel)]" options:0 metrics:nil views:viewsDictionary];


[self.view addConstraints:constraints];
[self.view addConstraints:constraints1];
[self.view addConstraints:constraints2];
[self.view addConstraints:constraints3];

现在您的 View 看起来不错。


在任何时候,要检查您的 View 是否缺少任何约束,请暂停调试器并在控制台中键入以下内容

po [[UIWindow keyWindow] _autolayoutTrace]

它将显示您的哪些观点有 AMBIGUOUS LAYOUT

此外,请记住在 Storyboard/IB 中,如果约束显示为“橙色”颜色,则您需要更多约束来定义对象位置。添加所有必要的约束后,约束颜色变为“蓝色”

关于ios - 在 UITableViewCell 中自动布局两个标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20485354/

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