![](/img/trans.png)
[英]UIImageView Ignores AutoLayout Constraints in UITableViewCell
[英]iOS 8 - AutoLayout issue - UIImageView inside UITableViewCell ignoring width constraints
我有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,因此如果文本短於120px,它甚至會調整大小。
現在發生的事情是:
| Labelxxxxx img1 img2 img3 img4 img5 |
| Labelxxxxx im.im.im.im.im. |
我沒有得到任何關於任何不可滿足的約束的異常或日志消息(即使對於40px約束,盡管圖像視圖被擠壓到10px)
提前感謝您提供的任何幫助!
- 編輯 -
我嘗試了一些替代方案,包括以編程方式設置約束,但看起來它們在使用IB時也按預期設置。
難道不可能設置兩個具有不同優先級的矛盾約束 - 只要具有更高優先級的約束需要被打破到更高約束的優勢,第二個應該被用作故障轉移?
(在我的情況下:標簽寬度=最高優先級,打破ImageView
寬度40px約束 - > ImageView
寬度0px作為故障轉移)
我可能會將IBOutlets連接到寬度約束並在旋轉時將寬度更改為0。
我有類似的問題,但通過以下方法解決
在UITableViewCell中添加了一個DummyView,並將DummyView的所有方面都添加到UITableViewCell。
現在在DummyView中添加所有圖像,標簽和其他子視圖。
為DummyView和您添加的子視圖創建IBOutLet。
以編程方式刪除所有約束
以編程方式添加基於設備方向的所有約束。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.