How do I find the available choices for anchorType in swift?

668 views Asked by At

As a beginner I'm trying to learn to properly navigate Apple's API documentation while studying a tutorial book as assistance. While following an example on programmatic constraints, I came across the following method:

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

I've learned that angle brackets signify a generic. However is this what Apple is implying by using angle brackets in this situation? While searching the documentation on NSLayoutAnchor, I couldn't find "topAnchor" similar to how the following code has:

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

How can I find out what are the choices for the generic type called "AnchorType"? Or am I looking at the context incorrectly?

Thank you in advance.

1

There are 1 answers

0
Gary Makin On

The first example you give is invalid code (the lack of balanced angle brackets, for one). However, I think I understand what it might be trying to explain.

Apple's documentation for NSLayoutXAxisAnchor (see https://developer.apple.com/documentation/uikit/nslayoutxaxisanchor) has a similar example to your second.

In this the documentation explains that the types of the anchors need to match. In the line

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

both leadingAnchor and trailingAnchor are X axis anchors and the code is good.

In the second example

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

topAnchor is a Y Axis anchor, so a warning is generated.

NSLayoutXAxisAnchor is derived from NSLayoutAnchor, so I think the first example you give is attempting to get across the idea that the parameter needs to be a NSLayoutAnchor that is actually a NSLayoutXAxisAnchor.

The documentation for NSLayoutAnchor (see https://developer.apple.com/documentation/uikit/nslayoutanchor) gives the possibilities:

  • NSLayoutXAxisAnchor
  • NSLayoutYAxisAnchor
  • NSLayoutDimension