无法在具有动态宽度的 Tablecell 中并排自动布局两个 UILabel
Cannot AutoLayout two UILabels side by side in Tablecell with dynamic widths
我有一个自定义单元格需要并排布置两个 UILabel。两个标签的宽度都是动态的,可以换行。
我定义的AutoLayout在以下情况下失效
- 滚动单元格及其包含部分,使其全部显示在屏幕上。
- 将设备旋转成横向(需要足够大的视图,以便 Cell 在屏幕上)
- 向后旋转,左侧标签将被压缩。
如果整个单元格和包含部分在旋转之前不在屏幕上,则布局不会失败
这是它工作和旋转后的几个屏幕截图
这是具有以下约束的布局
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));
如何停止挤压左侧标签?
将 UILabel 的内容拥抱优先级提高到 750 以上。
最初内容拥抱和内容压缩阻力优先级如下。
设置值如下
可以为这些设置任何值,但要根据您的优先级设置。
我有一个自定义单元格需要并排布置两个 UILabel。两个标签的宽度都是动态的,可以换行。
我定义的AutoLayout在以下情况下失效
- 滚动单元格及其包含部分,使其全部显示在屏幕上。
- 将设备旋转成横向(需要足够大的视图,以便 Cell 在屏幕上)
- 向后旋转,左侧标签将被压缩。
如果整个单元格和包含部分在旋转之前不在屏幕上,则布局不会失败
这是它工作和旋转后的几个屏幕截图
这是具有以下约束的布局
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));
如何停止挤压左侧标签?
将 UILabel 的内容拥抱优先级提高到 750 以上。
最初内容拥抱和内容压缩阻力优先级如下。
设置值如下
可以为这些设置任何值,但要根据您的优先级设置。