<!--
{
  "availability" : [
    "iOS: -",
    "iPadOS: -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.5.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "QuartzCore",
  "identifier" : "/documentation/QuartzCore/CAConstraint",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Core Animation"
    ],
    "preciseIdentifier" : "c:objc(cs)CAConstraint"
  },
  "title" : "CAConstraint"
}
-->

# CAConstraint

A representation of a single layout constraint between two layers.

```
class CAConstraint
```

## Overview

Each [`CAConstraint`](/documentation/QuartzCore/CAConstraint) instance encapsulates one geometry relationship between two layers on the same axis.

Sibling layers are referenced by name, using the name property of each layer. The special name `superlayer` is used to refer to the layer’s superlayer.

For example, to specify that a layer should be horizontally centered in its superview you would use the following:

```objc
theConstraint=[CAConstraint constraintWithAttribute:kCAConstraintMidX
                                         relativeTo:@"superlayer"
                                         attribute:kCAConstraintMidX];
```

A minimum of two relationships must be specified per axis. If you specify constraints for the left and right edges of a layer, the width will vary. If you specify constraints for the left edge and the width, the right edge of the layer will move relative to the superlayer’s frame. Often you’ll specify only a single edge constraint, the layer’s size in the same axis will be used as the second relationship.

> Important:
> It is possible to create constraints that result in circular references to the same attributes. In cases where the layout is unable to be computed the behavior is undefined.

## Topics

### Create a New Constraint

[`constraintWithAttribute:relativeTo:attribute:scale:offset:`](/documentation/QuartzCore/CAConstraint/constraintWithAttribute:relativeTo:attribute:scale:offset:)

Creates and returns an `CAConstraint` object with the specified parameters.

[`init(attribute:relativeTo:attribute:offset:)`](/documentation/QuartzCore/CAConstraint/init(attribute:relativeTo:attribute:offset:))

Creates and returns an `CAConstraint` object with the specified parameters.

[`init(attribute:relativeTo:attribute:)`](/documentation/QuartzCore/CAConstraint/init(attribute:relativeTo:attribute:))

Creates and returns an `CAConstraint` object with the specified parameters.

[`init(attribute:relativeTo:attribute:scale:offset:)`](/documentation/QuartzCore/CAConstraint/init(attribute:relativeTo:attribute:scale:offset:))

Returns an `CAConstraint` object with the specified parameters. Designated initializer.

### Accessing Constraint Values

[`attribute`](/documentation/QuartzCore/CAConstraint/attribute)

The attribute the constraint affects.

[`offset`](/documentation/QuartzCore/CAConstraint/offset)

Offset value of the constraint attribute.

[`scale`](/documentation/QuartzCore/CAConstraint/scale)

Scale factor of the constraint attribute.

[`sourceAttribute`](/documentation/QuartzCore/CAConstraint/sourceAttribute)

The constraint attribute of the layer the receiver is calculated relative to

[`sourceName`](/documentation/QuartzCore/CAConstraint/sourceName)

Name of the layer that the constraint is calculated relative to.

### Constants

[`CAConstraintAttribute`](/documentation/QuartzCore/CAConstraintAttribute)

The constraint attribute type.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)