我在使用 iOS 8 autolayout
时遇到问题。
我想要一个带有 UILabel
和 5 个 UIImageView
的自定义 UITableviewCell
。我希望图像以横向模式而不是纵向模式显示,因为那样 UILabel
会被压缩到窄。
我在 Interface Builder 中设计了 View 并添加了以下约束(以及其他约束):
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
设置为使图像居中并裁剪 subview ,并将内容压缩优先级设置为 1(最低)。
UILabel
content hugging priority 我也设置为 1,这样即使文本短于 120px,它也会调整大小。
现在发生的是:
- 横向模式:
一切正常
| Labelxxxxx img1 img2 img3 img4 img5 |
- 人像模式:
ImageView 被压缩到类似 10px 的大小(这意味着它打破了约束 #3(40px 宽度)以满足约束 #1(标签宽度),但它没有将 ImageView 宽度设置为 0px 这将满足约束 #4 )
| Labelxxxxx im.im.im.im.im. |
我没有收到任何关于任何不可满足约束的异常或日志消息(即使是 40px 约束也没有,尽管 ImageView 被压缩到 10px)
在此先感谢您提供的任何帮助!
--编辑--
我尝试了一些替代方法,包括以编程方式设置约束,但看起来它们在使用 IB 时也已按预期设置。
难道不能设置 2 个具有不同优先级的矛盾约束 - 一旦需要打破具有更高优先级的约束以获得更高约束的优势,第二个应该用作故障转移?
(在我的例子中:标签宽度 = 最高优先级,打破 ImageView
宽度 40px 约束 --> ImageView
宽度 0px 作为故障转移)
最佳答案
我可能会将 IBOutlets 连接到宽度约束并在旋转时将宽度更改为 0。
关于iOS 8 - AutoLayout 问题 - UITableViewCell 中的 UIImageView 忽略宽度限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25446084/