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

UIView that creates a sub UIView is generating hierarchy constraint warnings

我正在尝试在我的 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 init完成之前不能完成吗?

编辑:

Why are you initializing it with Sample views' aDecoder?

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

是的,可以在您的 init 中添加您的 contains。

您遇到的问题是关于何时添加约束与子视图有关。

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

这正是您的错误所指的内容:

 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,然后指定约束。

编辑

为什么要用 'Sample' views aDecoder 初始化它?你应该只在子类化时这样做

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

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

解决方案?在我的例子中,我使用 init(frame) 在视图的 init?(coder).

中创建了我想要的任何 UIView