如何以编程方式设置约束加上常量?

How to set constraint programatically plus constant?

我希望我的按钮高于中心 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) // -200 above the center?

用于引导安全区域

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

请参阅 Apple 文档中的 Link: https://developer.apple.com/documentation/uikit/nslayoutyaxisanchor

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

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