创建子UIView的UIView正在生成层次结构约束警告



我正试图在UIView中创建一个TGMapView。TMMapView一创建,我就收到这个错误:

2019-02-04 14:47:22.288241-0500 IOSDemoApp[8325:289276] [LayoutConstraints] The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x6000015f0b40 _UILayoutGuide:0x7fe52fc0e750.leading == TrimbleMaps.TMMapView:0x7fe52fc0e320.leading   (inactive)>
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.
2019-02-04 14:47:22.288477-0500 IOSDemoApp[8325:289276] [LayoutConstraints] The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x6000015f0c80 _UILayoutGuide:0x7fe52fc0e750.top == TrimbleMaps.TMMapView:0x7fe52fc0e320.top   (inactive)>
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.
2019-02-04 14:47:22.288642-0500 IOSDemoApp[8325:289276] [LayoutConstraints] The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x6000015f0e10 _UILayoutGuide:0x7fe52ff05670.leading == TrimbleMaps.TMMapView:0x7fe52fc0e320.leading   (inactive)>
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.
2019-02-04 14:47:22.310967-0500 IOSDemoApp[8325:289276] [LayoutConstraints] The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x6000015f0f00 TrimbleMaps.TMMapView:0x7fe52fc0e320.bottom == _UILayoutGuide:0x7fe52ff05670.bottom   (inactive)>
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.

我试图在这里创建的UIView是TGMapView,这似乎并不重要。如果我尝试创建一个普通的旧UIView,我会遇到同样的问题。

public class Sample: UIView {
private var tangramMapView: TGMapView!
public var trivialView: UIView!
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// both have the same warning/error
guard let tangramMapView = TGMapView(coder: aDecoder) else { return nil }
trivialView = UIView.init(coder: aDecoder)
setup()
}
}

当我运行这个时,TGMapView和UIView init都会发出相同的警告(复制到上面)。我希望没有任何警告,并且UIViews约束是在setup()中处理的。我在setup()中添加子视图并设置约束。

将UIViews添加到我不知道的UIViews有什么特别之处吗?我是不是顺序不对?在superView初始化完成之前,是否可以不这样做?

编辑:

为什么要用示例视图的aDecoder初始化它?

不知道。我在故事板中设置了它,并将单视图应用程序中的视图设置为Sample视图。在调试器中,我可以看到正在调用aDecoderinit。我无法告诉你为什么其他人没有。

是的,在init中添加包含内容是可以的。

您面临的问题是,何时添加约束是与子视图的关系。

在视图之间指定约束时,它们必须是彼此的子视图(一个在另一个之上),或者两者都是同一视图的子视图。

这就是你的错误所说的:

The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x6000015f0c80 _UILayoutGuide:0x7fe52fc0e750.top == TrimbleMaps.TMMapView:0x7fe52fc0e320.top   (inactive)>
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.

您正试图将TMMapView顶部约束关系指定为一个尚未与其相关的视图。

首先添加子视图,然后指定约束。

编辑

为什么要用"示例"视图aDecoder初始化它?只有在对进行子类化时才应该这样做

当我在两个月后忘记答案时,我正在为未来的用户和/或我自己回答我的问题。

init?(coder)用于在情节提要中设置UIView时创建UIView。UIView已打包,必须进行解码。重要的是,coder仅适用于在故事板中设置的UIView。在这种情况下,这是Sample。因此,使用coder来初始化TGMapView或任何其他类型的UIView是没有意义的。这就是我遇到的问题。

解决方案?在我的例子中,我使用init(frame)在视图的init?(coder)中创建我想要的任何UIView。

最新更新