Instance Method

constraint(lessThanOrEqualTo:)

Returns a constraint that defines one item’s attribute as less than or equal to another.

Declaration

func constraint(lessThanOrEqualTo anchor: NSLayoutAnchor<AnchorType>) -> NSLayoutConstraint

Parameters

anchor

A layout anchor from a UIView, NSView, or UILayoutGuide object. You must use a subclass of NSLayoutAnchor that matches the current anchor. For example, if you call this method on an NSLayoutXAxisAnchor object, this parameter must be another NSLayoutXAxisAnchor.

Return Value

An NSLayoutConstraint object that defines the attribute represented by this layout anchor as less than or equal to the attribute represented by the anchor parameter.

Discussion

This method defines the relationship first attribute <= second attribute. Where first attribute is the layout attribute represented by the anchor receiving this method call, and second attribute is the layout attribute represented by the anchor parameter. All values are measured in points; however, these values can be interpreted in different ways, depending on the type of layout anchor.

  • For leading or trailing anchors, the values increase as you move in the current language’s reading direction. For English, values increase as you move to the right.

  • For left and right anchors, the values increase as you move to the right.

  • For NSLayoutYAxisAnchor objects, the values increase as you move down.

  • For NSLayoutDimension objects, the values increase as the items increase in size.

The constraints produced by the following two examples are identical.

// Creating a constraint using NSLayoutConstraint
NSLayoutConstraint(item: subview,
                   attribute: .Leading,
                   relatedBy: .LessThanOrEqual,
                   toItem: view,
                   attribute: .LeadingMargin,
                   multiplier: 1.0,
                   constant: 0.0).active = true
 
// Creating the same constraint using constraintLessThanOrEqualToAnchor:
let margins = view.layoutMarginsGuide
subview.leadingAnchor.constraintLessThanOrEqualToAnchor(margins.leadingAnchor).active = true

See Also

Building Constraints

func constraint(equalTo: NSLayoutAnchor<AnchorType>) -> NSLayoutConstraint

Returns a constraint that defines one item’s attribute as equal to another.

func constraint(equalTo: NSLayoutAnchor<AnchorType>, constant: CGFloat) -> NSLayoutConstraint

Returns a constraint that defines one item’s attribute as equal to another item’s attribute plus a constant offset.

func constraint(greaterThanOrEqualTo: NSLayoutAnchor<AnchorType>) -> NSLayoutConstraint

Returns a constraint that defines one item’s attribute as greater than or equal to another.

func constraint(greaterThanOrEqualTo: NSLayoutAnchor<AnchorType>, constant: CGFloat) -> NSLayoutConstraint

Returns a constraint that defines one item’s attribute as greater than or equal to another item’s attribute plus a constant offset.

func constraint(lessThanOrEqualTo: NSLayoutAnchor<AnchorType>, constant: CGFloat) -> NSLayoutConstraint

Returns a constraint that defines one item’s attribute as less than or equal to another item’s attribute plus a constant offset.