【问题标题】:Cannot AutoLayout two UILabels side by side in Tablecell with dynamic widths无法在具有动态宽度的 Tablecell 中并排自动布局两个 UILabel
【发布时间】:2015-10-19 10:21:13
【问题描述】:

我有一个自定义单元格,需要并排布局两个 UILabel。两个标签的宽度都是动态的并且可以换行。

我定义的自动布局在以下情况下会失败

  1. 滚动单元格及其包含的部分,使其全部显示在屏幕上。
  2. 将设备旋转到横向(需要足够大的 View 以便单元格在屏幕上)
  3. 向后旋转,左侧标签将被压缩。

如果整个单元格和包含的部分在旋转之前不在屏幕上,则布局不会失败

这里是它工作和旋转后的几个屏幕截图

这是具有以下约束的布局

this.ContentView.AddConstraint(
    NSLayoutConstraint.Create(
        this.TextLabel, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this.ContentView, NSLayoutAttribute.Left,
        1.0f, (nfloat)LeftRightPadding));

this.ContentView.AddConstraint(
    NSLayoutConstraint.Create(
        this.TextLabel, NSLayoutAttribute.Top, NSLayoutRelation.Equal, this.ContentView, NSLayoutAttribute.Top,
        1.0f, (nfloat)TopAndBottomPadding));

this.ContentView.AddConstraint(
    NSLayoutConstraint.Create(
        this.TextLabel, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, this.ContentView, NSLayoutAttribute.Bottom,
        1.0f, 0 - (nfloat)TopAndBottomPadding));

this.ContentView.AddConstraint(
    NSLayoutConstraint.Create(
        this.DetailTextLabel, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this.ContentView, NSLayoutAttribute.Right,
        1.0f, 0 - (nfloat)LeftRightPadding));

this.ContentView.AddConstraint(
    NSLayoutConstraint.Create(
        this.DetailTextLabel, NSLayoutAttribute.Top, NSLayoutRelation.Equal, this.ContentView, NSLayoutAttribute.Top,
        1.0f, (nfloat)TopAndBottomPadding));

this.ContentView.AddConstraint(
    NSLayoutConstraint.Create(
        this.DetailTextLabel, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, this.ContentView, NSLayoutAttribute.Bottom,
        1.0f, 0 - (nfloat)TopAndBottomPadding));

this.ContentView.AddConstraint(
    NSLayoutConstraint.Create(
    this.DetailTextLabel, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this.TextLabel, NSLayoutAttribute.Right,
        1.0f,
        10));

如何阻止左侧标签的挤压?

【问题讨论】:

  • 增加左压缩阻力?

标签: ios xamarin.ios autolayout


【解决方案1】:

将 UILabel 的内容拥抱优先级提高到 750 以上。

最初,内容拥抱和内容压缩阻力优先级如下所示。

如下设置值

可以为这些设置任何值,但要根据您的优先级。

【讨论】:

  • 我想你的意思是图片中的Content Compression Resistance Priority
  • 我之前确实尝试过抗压。我现在会尝试拥抱和抵抗
  • 您说“高于 750”,但在图像中将它们设置为 750。 750够吗?
  • 拥抱和压缩都不起作用。我确实有屏幕上\屏幕外情况的更新,并将更新问题
  • @PatLong-MunkiiYebee - 抱歉,图片有点误导。我放置图像只是为了显示 IB 中的字段。您可以将内容拥抱优先级提高到抗压优先级之上。是否尝试过将内容拥抱优先级设置为 751(或以上),将内容抗压缩优先级设置为 250。
猜你喜欢
  • 1970-01-01
  • 2014-01-14
  • 1970-01-01
  • 2016-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多