0

我正在尝试在我的 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 初始化都会发出相同的警告(复制到上面)。我希望没有警告,并且 UIViews 约束在setup(). 我在setup().

将 UIViews 添加到我不知道的 UIViews有什么特别之处吗?我做错了顺序吗?在superView init完成之前不能完成吗?

编辑:

为什么要使用示例视图的 aDecoder 对其进行初始化?

不知道。我在情节提要中设置了它,并且在我的单视图应用程序中将视图设置为Sample视图。在调试器中,我可以看到aDecoder正在调用 init。我不能告诉你为什么其他人不是。

4

2 回答 2

1

是的,可以在 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顶部约束关系指定为尚未相对的视图。

首先AddSubviews,然后指定约束。

编辑

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

于 2019-02-04T20:21:16.747 回答
0

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

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

解决方案?就我而言,我曾经init(frame)在视图的init?(coder).

于 2019-02-07T14:55:27.637 回答