簡體   English   中英

Swift-約束-動態標簽寬度

[英]Swift - constraints - dynamic label width

我在一個“行”中有三個按鈕。

A-B --- C

我以編程方式添加了以下約束:

A有限制條件:

leading to superview (value = 16)
width (value = 49)
height (value = 40)
trailing to B (value = 16)
top to top layout guide (value = 16)

B有約束條件:

width (value = 49)
height (value = 40)
top to top layout guide (value = 16)

C有約束:

trailing to superview (value = 16)
height (value = 40)
top to top layout guide (value = 16)
leading to B (value >= 20)

現在leading to B如果我運行應用程序並抱怨“無法同時滿足約束條件”,則導致C中的leading to B導致錯誤。

我認為這是由於“缺少” C標簽的內容(稍后在運行時添加)導致的,因此寬度未知。

我嘗試手動添加C:

width (value = 20) 

“錯誤”仍然存在,但這應滿足導致B> = 20的條件。

However, If I run app, even though the error is printed, the UI looks correct. What is the problem / solution for this?

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. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x1c4098ab0 h=--& v=--& UIView:topView.width == 0   (active)>",
    "<NSLayoutConstraint:0x1c4092480 H:[UIView:parentParent]-(0)-|   (active, names: '|':UIView:topView )>",
    "<NSLayoutConstraint:0x1c40924d0 UIVIew:topView.leading == UIView:parentParent.leading   (active)>",
    "<NSLayoutConstraint:0x1c0094140 H:|-(0)-[UIView:parent](LTR)   (active, names: '|':UIView:parentParent )>",
    "<NSLayoutConstraint:0x1c00940a0 UIView:parent.right == UIView:parentParent.right   (active)>",
    "<NSLayoutConstraint:0x1c0094000 H:|-(16)-[UIButton:A]   (active, names: '|':UIView:parent )>",
    "<NSLayoutConstraint:0x1c0093f60 UIButton:A.trailing == UIButton:B.leading - 16   (active)>",
    "<NSLayoutConstraint:0x1c0094730 H:[UIButton:B]-(>=20)-[UIButton:C]   (active)>",
    "<NSLayoutConstraint:0x1c0094780 UIButton:C.trailing == UIView:parent.trailing - 16   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x1c0094730 H:[UIButton:B]-(>=20)-[UIButton:C]   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

其中A,B,C是按鈕, parent是其父視圖。 整個父視圖在parentParent內部,所有視圖都在top視圖中。

您的視圖已將自動調整大小的蒙版轉換為約束,請嘗試將其刪除:

button.translatesAutoresizingMaskIntoConstraints = false

我已經測試了您的約束條件,但沒有看到錯誤。

不幸的是,運行時的控制台日志不是很有趣,如果您使用Xcode> = 7,則可以為約束設置標識符,選擇標識符並轉到“屬性檢查器”,如下圖所示:

在此處輸入圖片說明

在此之后,您的日志更具可讀性。

如果遇到問題,可以編輯帶有完整錯誤日志的帖子。

編輯:

父視圖會出現問題。

在此圖像中,您可以看到我的測試(日志中沒有錯誤),我僅將高度約束添加到parent和parentParent中,您可以檢查差異。

在此處輸入圖片說明

暫無
暫無

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

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