【问题标题】:How do I find the available choices for anchorType in swift?如何在 swift 中找到可用的 anchorType 选项?
【发布时间】:2018-04-24 21:55:00
【问题描述】:

作为一名初学者,我正在努力学习如何正确浏览 Apple 的 API 文档,同时学习一本教程书作为帮助。在遵循有关编程约束的示例时,我遇到了以下方法:

let leadingConstraint = segmentedControl.leadingAnchor.constraint(equalTo: <NSLayoutAnchor<NSLayoutXAxisAnchor>)

我了解到尖括号表示泛型。然而,这是苹果在这种情况下使用尖括号所暗示的吗?在 NSLayoutAnchor 上搜索文档时,我找不到类似于以下代码的“topAnchor”:

let topConstraint = segmentedControl.topAnchor.constraint(equalTo: view.topAnchor)

我怎样才能找到名为“AnchorType”的泛型类型的选择?还是我看错了上下文?

提前谢谢你。

【问题讨论】:

    标签: ios swift apple-documentation


    【解决方案1】:

    您给出的第一个示例是无效代码(例如缺少平衡的尖括号)。不过,我想我理解它可能试图解释的内容。

    Apple 的 NSLayoutXAxisAnchor 文档(请参阅 https://developer.apple.com/documentation/uikit/nslayoutxaxisanchor)有一个与您的第二个类似的示例。

    在此文档中解释了锚的类型需要匹配。在行中

    cancelButton.leadingAnchor.constraintEqualToAnchor(saveButton.trailingAnchor, constant: 8.0).active = true
    

    leadingAnchortrailingAnchor都是X轴锚点,代码不错。

    在第二个例子中

    cancelButton.leadingAnchor.constraintEqualToAnchor(saveButton.topAnchor, constant: 8.0).active = true
    

    topAnchor 是 Y 轴锚点,因此会生成警告。

    NSLayoutXAxisAnchor 派生自 NSLayoutAnchor,所以我认为您给出的第一个示例是试图理解参数必须是 NSLayoutAnchor,实际上是 NSLayoutXAxisAnchor

    NSLayoutAnchor 的文档(请参阅https://developer.apple.com/documentation/uikit/nslayoutanchor)提供了可能性:

    • NSLayoutXAxisAnchor
    • NSLayoutYAxisAnchor
    • NSLayoutDimension

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-24
      • 1970-01-01
      • 2014-07-26
      相关资源
      最近更新 更多