Instance Property

contentsGravity

A constant that specifies how the layer's contents are positioned or scaled within its bounds.

Declaration

var contentsGravity: CALayerContentsGravity { get set }

Discussion

The possible values for this property are listed in Contents Gravity Values.

The default value of this property is resize.

Figure 1 shows four examples of the effect of setting different values for a layer's contentsGravity property.

Figure 1

Different effects of setting a layer's contents gravity

Different effects of setting a layer's contents gravity
  1. Contents gravity is resize - the default

  2. Contents gravity is center

  3. Contents gravity is contentsAreFlipped() ? top : bottom

  4. Contents gravity is contentsAreFlipped() ? bottomLeft : topLeft

See Also

Modifying the Layer’s Appearance

Contents Gravity Values

The contents gravity constants specify the position of the content object when the layer bounds is larger than the bounds of the content object. They are used by the contentsGravity property.

var opacity: Float

The opacity of the receiver. Animatable.

var isHidden: Bool

A Boolean indicating whether the layer is displayed. Animatable.

var masksToBounds: Bool

A Boolean indicating whether sublayers are clipped to the layer’s bounds. Animatable.

var mask: CALayer?

An optional layer whose alpha channel is used to mask the layer’s content.

var isDoubleSided: Bool

A Boolean indicating whether the layer displays its content when facing away from the viewer. Animatable.

var cornerRadius: CGFloat

The radius to use when drawing rounded corners for the layer’s background. Animatable.

var borderWidth: CGFloat

The width of the layer’s border. Animatable.

var borderColor: CGColor?

The color of the layer’s border. Animatable.

var backgroundColor: CGColor?

The background color of the receiver. Animatable.

var shadowOpacity: Float

The opacity of the layer’s shadow. Animatable.

var shadowRadius: CGFloat

The blur radius (in points) used to render the layer’s shadow. Animatable.

var shadowOffset: CGSize

The offset (in points) of the layer’s shadow. Animatable.

var shadowColor: CGColor?

The color of the layer’s shadow. Animatable.

var shadowPath: CGPath?

The shape of the layer’s shadow. Animatable.

var style: [AnyHashable : Any]?

An optional dictionary used to store property values that aren't explicitly defined by the layer.

var allowsEdgeAntialiasing: Bool

A Boolean indicating whether the layer is allowed to perform edge antialiasing.

var allowsGroupOpacity: Bool

A Boolean indicating whether the layer is allowed to composite itself as a group separate from its parent.