簡體   English   中英

iOS-表格視圖單元格中的自動布局問題

[英]iOS - Autolayout issue in table view cell

我有一個包含以下項目的表格視圖單元:

  • 影像檢視(343 x 193)
  • 標簽1(高度:17)
  • 標簽2(高度:20)

無論設備是什么,我都希望圖像保持相同的縱橫比(此應用程序僅適用於iPhone,不適用於iPad)。 我正在做的是添加以下內容:

  • 圖像視圖:長寬比約束; 上,左,右約束

  • 標簽1:高度限制; 頂部(相對於圖像視圖),左側,
    正確的約束; 相等的寬度(帶有圖像視圖)

  • 標簽2:高度限制; 頂部(相對於圖像視圖),左側,
    右,下(關於單元格)約束; 等寬(
    圖片視圖)

當我測試這個時,我得到以下內容

2018-04-14 11:50:33.457531-0500 MyAppIOS[4357:479920] [LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<NSLayoutConstraint:0x60000009f9a0 UIImageView:0x7ff04952dfb0.width == 1.7772*UIImageView:0x7ff04952dfb0.height   (active)>",
    "<NSLayoutConstraint:0x60000009fc70 UILabel:0x7ff04952e1e0.height == 17   (active)>",
    "<NSLayoutConstraint:0x60000009fd60 UILabel:0x7ff04952e940'Loading...'.height == 20   (active)>",
    "<NSLayoutConstraint:0x60000009fea0 V:|-(10)-[UIImageView:0x7ff04952dfb0]   (active, names: '|':UITableViewCellContentView:0x7ff04952d770 )>",
    "<NSLayoutConstraint:0x60000009fef0 H:[UIImageView:0x7ff04952dfb0]-(16)-|   (active, names: '|':UITableViewCellContentView:0x7ff04952d770 )>",
    "<NSLayoutConstraint:0x60000009ff40 H:|-(16)-[UIImageView:0x7ff04952dfb0]   (active, names: '|':UITableViewCellContentView:0x7ff04952d770 )>",
    "<NSLayoutConstraint:0x60000009ff90 V:[UIImageView:0x7ff04952dfb0]-(8)-[UILabel:0x7ff04952e1e0]   (active)>",
    "<NSLayoutConstraint:0x600000280140 V:[UILabel:0x7ff04952e1e0]-(8)-[UILabel:0x7ff04952e940'Loading...']   (active)>",
    "<NSLayoutConstraint:0x6000002801e0 V:[UILabel:0x7ff04952e940'Loading...']-(10)-|   (active, names: '|':UITableViewCellContentView:0x7ff04952d770 )>",
    "<NSLayoutConstraint:0x60800009c070 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7ff04952d770.height == 266   (active)>",
    "<NSLayoutConstraint:0x60800009e230 'UIView-Encapsulated-Layout-Width' UITableViewCellContentView:0x7ff04952d770.width == 375   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x60000009fd60 UILabel:0x7ff04952e940'Loading...'.height == 20   (active)>

因此,基本上說這將修改第二個標簽的高度以適應。

有沒有更好的方法來設置這些約束? 我應該比正在使用的更多/更少嗎?

該沖突背后的原因是,該單元格最初假設一個靜態高度約束,該約束與子視圖中的當前約束設置發生沖突。 UIView-Encapsulated-Layout-Height很清楚地看到

因此,將label2的底部約束的優先級設置為999。

(此外,如果設置左右約束,則不要設置寬度約束。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM