【问题标题】:How to set constraint programatically plus constant?如何以编程方式设置约束加常量?
【发布时间】:2020-01-10 20:03:12
【问题描述】:

我希望我的按钮在中心上方 200 处。我的代码错误:

docButton.centerYAnchor.constraint(equalTo: view.centerYAnchor + 200),

Binary operator '+' cannot be applied to operands of type 'NSLayoutYAxisAnchor' and 'Int'

如何将按钮设置为在中心上方一些位置?

另外,我想设置一个后退按钮。如何将按钮的约束设置为顶部安全区域下方 20 点和左侧安全区域右侧 20 点?

提前致谢。

【问题讨论】:

  • docButton.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 200)

标签: ios swift xcode autolayout constraints


【解决方案1】:

你需要

docButton.centerYAnchor.constraint(equalTo: view.centerYAnchor,constant:-200) // -200 above the center?

用 safeArea 引导

docButton.leadingnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingnchor,constant:20)

【讨论】:

  • 非常感谢。关于我手机的安全视图(边界)如何?
【解决方案2】:

请参阅 Apple Docs 上的此链接: https://developer.apple.com/documentation/uikit/nslayoutyaxisanchor

  1. 您正在尝试将 200 添加到非整数类型 (NSLayoutYAxisAnchor)。

  2. 你可以使用这个: cancelButton.topAnchor.constraintEqualToAnchor(saveButton.topAnchor, constant: -200).active = true

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多