2015-09-27 79 views
0

我的應用程序在轉換某些UIViews時崩潰了,我無法找到原因。我在Xcode的日誌控制檯收到此錯誤:無法調試佈局約束

2015年9月23日10:03:43.420我的應用程序[1510:528283]視圖層次不用於約束準備:當添加到一個視圖,約束的項目必須是該視圖的後代(或視圖本身)。如果在組裝視圖層次結構之前需要解析約束,這將會崩潰。打破 - [UIView(UIConstraintBasedLayout)_viewHierarchyUnpreparedForConstraint:]進行調試。

2015年9月23日10:03:43.421我的應用程序[1510:528283]在***斷言失敗 - [UIView的_layoutEngine_didAddLayoutConstraint:roundingAdjustment:mutuallyExclusiveConstraints:],/BuildRoot/Library/Caches/com.apple.xbs /Sources/UIKit/UIKit-3505.16/NSLayoutConstraint_UIKitAdditions.m:590

我不知道如何調試這一點,我不明白,Break on -[UIView(UIConstraintBasedLayout) _viewHierarchyUnpreparedForConstraint:] to debug.。我沒有找到如何在文檔和任何示例中做到這一點,有人可以告訴我,我該怎麼辦?

+0

需要您用來添加約束 – dopcn

回答

0

當您在視圖之間添加約束時,兩個視圖必須位於同一層次結構中。

這意味着這兩個視圖必須共享一個共同的超級景觀。

檢查您的約束代碼以確保是這種情況。

+0

感謝代碼。我怎麼找到和debig像這樣:'視圖層次沒有爲約束準備:'? – AppsDev

+0

你添加了一個斷點嗎? – Abizern

+0

是的,但我有時會得到這個錯誤,有時我沒有,我無法找到什麼約束導致與「正常」斷點崩潰... – AppsDev

2

例如,當您在視圖上添加約束並且視圖本身尚未添加到其他視圖時,會發生這種情況。沒有道理

override func viewDidLoad() { 
    super.viewDidLoad 

    let newView = setupCandyView() 
    view.addSubview(newView) 
} 


func setupCandyView() -> UIView { 
    let container = UIView() 

    let button = UIButton() 
    container.addSubview(button) 

    // you are adding constarints here 
    // but container isn't added to the view yet 

    addConstraint(NSLayoutConstraint(item: button, 
    attribute: .CenterY, 
    relatedBy: .Equal, 
    toItem: container, 
    attribute: .CenterY, 
    multiplier: 1.0, 
    constant: 0)) 

    return container 
} 

只需添加一個符號斷點來捕獲錯誤發生的位置。

Symbolic Breakpoint Example