【发布时间】:2014-10-16 06:03:56
【问题描述】:
我在使用 iOS 8 autolayout 时遇到问题。
我想要一个带有UILabel 和5 个UIImageView 的自定义UITableviewCell。我希望图像以横向模式而不是以纵向模式显示,因为那样UILabel 会被挤压变窄。
我在界面生成器中设计了视图并添加了以下约束(以及其他):
1. priority 1000: Label width >= 120px
2. priority 1000: ImageView[1..5]
horizontal space to next one = 0px (to align them next to each
other, the rightmost image view got horizontal space = 0 to content
view to align them to the right side)
3. priority 999: ImageView[1..5]
width = 40px
4. priority 998: ImageView[1..5] width = 0px (hide image
views if the 40px constraint can't be satisfied because of the
required label width)
我还将ImageView 设置为图像居中并剪切子视图,并将内容压缩优先级设置为 1(最低)。
UILabel 内容拥抱优先级我也设置为 1,这样如果文本短于 120 像素,它甚至会调整大小。
现在发生的是:
- 横向模式:
一切正常| Labelxxxxx img1 img2 img3 img4 img5 | - 人像模式:
图像视图被压缩到 10px 之类的东西(这意味着它打破了约束 #3(40px 宽度)以满足约束 #1(标签宽度),但它没有将图像视图宽度设置为 0px,这将满足约束 #4 )| Labelxxxxx im.im.im.im.im. |
我没有收到任何关于任何不可满足的约束的异常或日志消息(即使是 40px 约束,尽管图像视图被压缩到 10px)
提前感谢您提供的任何帮助!
--编辑--
我尝试了一些替代方法,包括以编程方式设置约束,但看起来它们在使用 IB 时也按预期设置。
难道不应该设置两个具有不同优先级的矛盾约束吗?只要需要打破具有更高优先级的约束以利用更高的约束,就应该将第二个约束用作故障转移?
(在我的情况下:标签宽度 = 最高优先级,打破 ImageView 宽度 40px 约束 --> ImageView 宽度 0px 作为故障转移)
【问题讨论】:
-
我有同样的问题。找到解决方案 [这里][1] [1]:stackoverflow.com/a/18129840/3996629
-
现在解决了吗?还是一个悬而未决的问题?
-
您可以尝试使用其他优先级,例如 750 和 250。我看到他们经常做不同的事情。
标签: ios objective-c uiimageview autolayout ios8