簡體   English   中英

iOS自動布局:在viewDidLayoutSubviews中設置約束?

[英]iOS autolayout: set constraints in viewDidLayoutSubviews?

我有一個xib view controller xib包含所有無約束的子視圖,我需要手動添加這些約束。 當我在viewDidLoad編寫代碼時,一切都正確。 但是我需要獲得一個size參數,該參數僅在viewDidLayoutSubviews可用。 當我嘗試在viewDidLayoutSubviews調用代碼時,出現以下錯誤:

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:0x79edebb0 V:|-(50)-[UILabel:0x79f11bf0'You haven't imported any ...']   (Names: '|':UIView:0x79f11940 )>",
    "<NSLayoutConstraint:0x79e4b190 V:|-(49)-[UILabel:0x79f11bf0'You haven't imported any ...']   (Names: '|':UIView:0x79f11940 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x79edebb0 V:|-(50)-[UILabel:0x79f11bf0'You haven't imported any ...']   (Names: '|':UIView:0x79f11940 )>

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.

約束很簡單:所有視圖都放置在一列中,固定寬度,固定高度,最頂層視圖與超級視圖頂部邊框之間的距離,相鄰視圖之間的距離,最后所有視圖都捕捉到超級視圖的水平中心。

更新

我找到了問題的原因,但我不知道為什么它不起作用。

問題是最高的約束值基於視圖的邊界 bounds結構內的高度值“跳” 1(因為多次調用viewDidLayoutSubviews )。 但是,為什么它會引起約束問題呢?

它說UILabel中的1個具有約束,這會破壞布局。 2個選擇

  1. 清除所有標簽的約束,並通過嘗試n錯誤設置每個標簽的約束
  2. 將id設置為uilabel的所有約束,然后您將在

警告或錯誤輸出。

基本上是由於您添加了不必要的約束。

暫無
暫無

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

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