gpt4 book ai didi

ios - 无法调试布局约束

转载 作者:行者123 更新时间:2023-11-28 19:44:42 26 4
gpt4 key购买 nike

我的应用程序在转换某些 UIViews 时崩溃,我找不到原因。我在 Xcode 的日志控制台中收到此错误:

2015-09-23 10:03:43.420 My App[1510:528283] The view hierarchy is not prepared for the constraint: When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView(UIConstraintBasedLayout) _viewHierarchyUnpreparedForConstraint:] to debug.

2015-09-23 10:03:43.421 My App[1510:528283] *** Assertion failure in -[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:] 进行调试。。我没有在文档和任何示例中找到如何执行此操作,有人可以告诉我该怎么做吗?

最佳答案

例如,当您在 View 上添加约束并且 View 本身尚未添加到另一个 View 时,就会发生这种情况。没有意义

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

关于ios - 无法调试布局约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32808172/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com