Galen BlueTalon
Galen BlueTalon

Reputation: 193

How to set constraint programatically plus constant?

I want my button to be 200 above the center. My code errors:

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

with

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

How can I set a button to be some amount above the center?

Also, I want to set a single back button. How can I set a button's constraints to be 20 points below the top safe area and 20 points right of the left safe area?

Thanks in advance.

Upvotes: 0

Views: 526

Answers (2)

Shehata Gamal
Shehata Gamal

Reputation: 100503

You need

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

For leading with safeArea

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

Upvotes: 1

AppleCiderGuy
AppleCiderGuy

Reputation: 1297

Please refer to this Link on Apple Docs: https://developer.apple.com/documentation/uikit/nslayoutyaxisanchor

  1. you are trying to add 200 to a non-Integer type (NSLayoutYAxisAnchor).

  2. you can use this: cancelButton.topAnchor.constraintEqualToAnchor(saveButton.topAnchor, constant: -200).active = true

Upvotes: 1

Related Questions