CAConstraint Class Reference

Inherits from
Conforms to
Framework
/System/Library/Frameworks/QuartzCore.framework
Availability
Available in OS X v10.5 and later.
Declared in
CAConstraintLayoutManager.h
Companion guides
Related sample code

Overview

CAConstraint represents a single layout constraint between two layers. Each 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:

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

A maximum 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.

Tasks

Create a New Constraint

Accessing Constraint Values

Properties

attribute

The attribute the constraint affects. (read-only)

@property(readonly) CAConstraintAttribute attribute
Availability
  • Available in OS X v10.6 and later.
Declared In
CAConstraintLayoutManager.h

offset

Offset value of the constraint attribute. (read-only)

@property(readonly) CGFloat offset
Availability
  • Available in OS X v10.6 and later.
Declared In
CAConstraintLayoutManager.h

scale

Scale factor of the constraint attribute. (read-only)

@property(readonly) CGFloat scale
Availability
  • Available in OS X v10.6 and later.
Declared In
CAConstraintLayoutManager.h

sourceAttribute

The constraint attribute of the layer the receiver is calculated relative to (read-only)

@property(readonly) CAConstraintAttribute sourceAttribute
Availability
  • Available in OS X v10.6 and later.
Declared In
CAConstraintLayoutManager.h

sourceName

Name of the layer that the constraint is calculated relative to. (read-only)

@property(readonly) NSString *sourceName
Availability
  • Available in OS X v10.6 and later.
Declared In
CAConstraintLayoutManager.h

Class Methods

constraintWithAttribute:relativeTo:attribute:

Creates and returns an CAConstraint object with the specified parameters.

+ (id)constraintWithAttribute:(CAConstraintAttribute)attr relativeTo:(NSString *)srcLayer attribute:(CAConstraintAttribute)srcAttr
Parameters
attr

The attribute of the layer for which to create a new constraint.

srcLayer

The name of the layer that this constraint is calculated relative to.

srcAttr

The attribute of srcLayer the constraint is calculated relative to.

Return Value

A new CAConstraint object with the specified parameters. The scale of the constraint is set to 1.0. The offset of the constraint is set to 0.0.

Discussion

The value for the constraint is calculated is srcAttr.

Availability
  • Available in OS X v10.5 and later.
Related Sample Code
Declared In
CAConstraintLayoutManager.h

constraintWithAttribute:relativeTo:attribute:offset:

Creates and returns an CAConstraint object with the specified parameters.

+ (id)constraintWithAttribute:(CAConstraintAttribute)attr relativeTo:(NSString *)srcLayer attribute:(CAConstraintAttribute)srcAttr offset:(CGFloat)offset
Parameters
attr

The attribute of the layer for which to create a new constraint.

srcLayer

The name of the layer that this constraint is calculated relative to.

srcAttr

The attribute of srcLayer the constraint is calculated relative to.

offset

The offset added to the value of srcAttr.

Return Value

A new CAConstraint object with the specified parameters. The scale of the constraint is set to 1.0.

Discussion

The value for the constraint is calculated as (srcAttr + offset).

Availability
  • Available in OS X v10.5 and later.
Related Sample Code
Declared In
CAConstraintLayoutManager.h

constraintWithAttribute:relativeTo:attribute:scale:offset:

Creates and returns an CAConstraint object with the specified parameters.

+ (id)constraintWithAttribute:(CAConstraintAttribute)attr relativeTo:(NSString *)srcLayer attribute:(CAConstraintAttribute)srcAttr scale:(CGFloat)scale offset:(CGFloat)offset
Parameters
attr

The attribute of the layer for which to create a new constraint.

srcLayer

The name of the layer that this constraint is calculated relative to.

srcAttr

The attribute of srcLayer the constraint is calculated relative to.

scale

The amount to scale the value of srcAttr.

offset

The offset from the srcAttr.

Return Value

A new CAConstraint object with the specified parameters.

Discussion

The value for the constraint is calculated as ((srcAttr * scale) + offset).

Availability
  • Available in OS X v10.5 and later.
Declared In
CAConstraintLayoutManager.h

Instance Methods

initWithAttribute:relativeTo:attribute:scale:offset:

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

- (id)initWithAttribute:(CAConstraintAttribute)attr relativeTo:(NSString *)srcLayer attribute:(CAConstraintAttribute)srcAttr scale:(CGFloat)scale offset:(CGFloat)offset
Parameters
attr

The attribute of the layer for which to create a new constraint.

srcLayer

The name of the layer that this constraint is calculated relative to.

srcAttr

The attribute of srcLayer the constraint is calculated relative to.

scale

The amount to scale the value of srcAttr.

offset

The offset added to the value of srcAttr.

Return Value

An initialized constraint object using the specified parameters.

Discussion

The value for the constraint is calculated as (srcAttr * scale) + offset).

Availability
  • Available in OS X v10.5 and later.
Declared In
CAConstraintLayoutManager.h

Constants

CAConstraintAttribute

These constants represent the geometric edge or axis of a constraint.

enum _CAConstraintAttribute
{
   kCAConstraintMinX,
   kCAConstraintMidX,
   kCAConstraintMaxX,
   kCAConstraintWidth,
   kCAConstraintMinY,
   kCAConstraintMidY,
   kCAConstraintMaxY,
   kCAConstraintHeight,
};
Constants
kCAConstraintMinX

The left edge of a layer’s frame.

Available in OS X v10.5 and later.

Declared in CAConstraintLayoutManager.h.

kCAConstraintMidX

The horizontal location of the center of a layer’s frame.

Available in OS X v10.5 and later.

Declared in CAConstraintLayoutManager.h.

kCAConstraintMaxX

The right edge of a layer’s frame.

Available in OS X v10.5 and later.

Declared in CAConstraintLayoutManager.h.

kCAConstraintWidth

The width of a layer.

Available in OS X v10.5 and later.

Declared in CAConstraintLayoutManager.h.

kCAConstraintMinY

The bottom edge of a layer’s frame.

Available in OS X v10.5 and later.

Declared in CAConstraintLayoutManager.h.

kCAConstraintMidY

The vertical location of the center of a layer’s frame.

Available in OS X v10.5 and later.

Declared in CAConstraintLayoutManager.h.

kCAConstraintMaxY

The top edge of a layer’s frame.

Available in OS X v10.5 and later.

Declared in CAConstraintLayoutManager.h.

kCAConstraintHeight

The height of a layer.

Available in OS X v10.5 and later.

Declared in CAConstraintLayoutManager.h.

Declared In
CAConstraint.h

Constraint Attribute Type

The constraint attribute type.

typedef int CAConstraintAttribute;
Availability
  • Available in OS X v10.5 and later.
Declared In
CAConstraintLayoutManager.h