Skip to main content
Version: 4.64

Layout

Layouts combine layers to define the appearance and structure of an application's interface. By positioning and sizing layers, you can create individual components and complete screens.

Tutorial video​

This video provides a concise overview of layouts in mos. It covers the underlying concepts and places special emphasis on constraints.

Layout files​

./layout directory​

Layer style files are stored in the ./layout directory.

./layout/
├── button.json
└── homeView.json

Layout file structure​

A layout consists of a name field and a layers array. The name identifies the layout and must match the filename.

Each entry in the layers array defines a layer. The order of the layers determines their stacking order: layers listed later appear above layers listed earlier.

{
"name": "button",
"layers": [
{
"type": "color"
},
{
"type": "text"
}
]
}
The app showing the text layer with the applied layer style text.The app showing the text layer with the applied layer style text.
The app showing the color layer with the text layer on top of it.The app showing the color layer with the text layer on top of it.
note

Layers listed later in the array appear higher in the layer stack. This allows layers to overlap, for example, to display text on top of a button.

Basic attributes​

Every layer requires a type attribute, which defines the kind of layer. Layers also share a set of common attributes for appearance, behavior, and state.

KeyTypeDescriptionDefault value
actionsActionListActions executed when the layer is tapped.null
borderColorColorBorder color specified as a hexadecimal color value.null
borderRadiusUnitSizeRadius applied to all corners. The radius is set even when borderWidth is 0, in which case no border is drawn.0
borderWidthUnitSizeWidth of the border.0
bottomLeftRadiusUnitSizeRadius of the bottom-left corner.0
bottomRightRadiusUnitSizeRadius of the bottom-right corner.0
classesArray[String]Class selectors used by layer styles.null
conditionsConditionsConditions that determine the layer state.null
constraintsConstraintsConstraints defining the layer's position and size.null
consumesKeyboardBooleanIndicates whether the layer shrinks when the keyboard is displayed.null
dataKeyStringKey used to assign data to the layer.null
hiddenBooleanIndicates whether the layer is hidden.false
layerRotationNumberRotation angle of the layer in degrees.0
nameStringUnique name of the layer within the layout, used for alignment or actions.null
opacityNumberOpacity of the layer.1
overInsetBottomBooleanDetermines whether the layer extends into the bottom safe area of the device.false
overInsetTopBooleanDetermines whether the layer extends into the top safe area of the device.false
safeAreaSafeAreaTypeObjectSafe area configuration. See the linked type for details.null
shadowElevationNumberElevation used to cast shadows.0
stateStringDefault state of the layer."default"
statesLayerStatesNamed states that affect the layer's appearance.null
topLeftRadiusUnitSizeRadius of the top-left corner.0
topRightRadiusUnitSizeRadius of the top-right corner.0
touchFeedbackTouchFeedbackDetermines whether touch feedback is displayed when the layer is touched."visible"
trackingKeyStringKey used to identify a scroll event.null
valueValueInitial value of the layer.null

Layout basics​

Each layer is treated as a rectangle in mos. Its position and size can be defined dyanmically using a grid or by defining an absolute or relative position and size using anchors.

Anchor basics​

Anchors define the position of a layer relative to the screen or another layer. They can also be used to determine the layer's size.

The available anchors are:

AnchorDescription
nNorth
eEast
sSouth
wWest
cCenter
nwNorth-west
neNorth-east
seSouth-east
swSouth-west

In mos., the n, e, s, and w anchors represent edges rather than individual points.

Visualization of anchors in mos. Visualization of anchors in mos.
note

The top-left corner is the default starting point in mos. Therefore, the nw anchor is used by default and does not need to be specified explicitly.

Anchoring​

Anchors can attach one edge or point of a layer to an anchor on another layer. The anchor attribute defines the anchor on the current layer, while relativeAnchor defines the corresponding anchor on the reference layer.

note

The n, e, s, and w anchors represent edges rather than fixed points. For example, attaching the w anchor of one layer to the e anchor of another does not determine the vertical position along the shared edges.

Layout constraints​

Constraints define the position and size of a layer. The appropriate constraint depends on the intended layout and how it should adapt to different devices and screen sizes.

Dimensions can be specified in pt, dp, %, or px. See UnitSize for details.

pos constraint​

The pos constraint positions a layer relative to the screen, the current view, or another layer.

KeyDescriptionDefault value
xHorizontal offset from the reference point. Positive values move the layer right; negative values move it left.null
yVertical offset from the reference point. Positive values move the layer down; negative values move it up.null
anchorAnchor on the current layer to which the position values are applied.nw
relativeAnchorAnchor on the reference layer. Defaults to the top-left anchor of the screen or surrounding layer.nw
relativeToName of the layer used as the positioning reference.null
{
"type": "pos",
"x": "12pt",
"y": "6pt",
"anchor": "sw",
"relativeAnchor": "ne",
"relativeTo": "myLayer"
}

size constraint​

The size constraint defines the dimensions of a layer.

KeyDescriptionDefault value
widthWidth of the layer in dp, pt, or %.null
heightHeight of the layer in dp, pt, or %.null
relativeToName of the layer used as the sizing reference.null
{
"type": "size",
"height": "240pt",
"width": "144pt"
}

grid constraint​

The grid constraint is used for layers inside a grid. It defines how many columns a layer occupies and how its height is determined.

KeyDescriptionDefault value
colSpanNumber of grid columns occupied by the layer.null
aspectRatioHeight of the layer relative to its width.null
heightHeight of the layer in dp, pt, or %.null
{
"type": "grid",
"colSpan": "4",
"aspectRatio": "2"
}
note

Specify only one of the vertical sizing attributes, height or aspectRatio, at a time.

aspect constraint​

The aspect constraint defines a layer's size based on its aspect ratio in relation to a size specified by another constraint.

KeyDescriptionDefault value
ratioHeight of the layer relative to its width.null
{
"type": "aspect",
"ratio": "1.25"
}
note

A ratio of 1 represents a 1:1 aspect ratio, resulting in a square.

The special ratio value content can be used with image layers to preserve the image's original aspect ratio.

This constraint is supported only by certain layer types, such as image layers.

Advanced sizing​

Dynamic content sizing​

Fixed dimensions can cause content to be cut off or leave gaps when content length or aspect ratios vary. Text and container layers can be configured to adjust their size dynamically based on their content.

The following example defines the horizontal boundaries of a text layer:

{
"name": "myText",
"type": "text",
"value": "This text will differ in length depending on the response of something outside the app.",
"constraints": [
{
"type": "pos",
"x": "0pt",
"y": "0pt"
},
{
"type": "pos",
"anchor": "e",
"relativeAnchor": "e",
"x": "0pt"
}
]
}

The top-left anchor defines the layer's origin, while the east anchor defines its right edge. If the content exceeds the available width, the text wraps onto additional lines until all content is displayed, provided that lineBreakMode is configured accordingly in the text layer.

Anchor spanning/corner pinning​

A layer's size can also be defined by pinning two opposite corners of its rectangle. This sets its boundaries in both dimensions.

Layout examples​

The following examples demonstrate different ways to position and size layers.

Using Relative positioning​

This example positions text at the center of a button by attaching the center anchor of the text layer to the center anchor of the button layer.

The following snippet shows two layer objects in a layout:

[
{
"type": "color",
"name": "button",
"constraints": [
{
"type": "pos",
"x": "0",
"y": "0"
},
{
"type": "size",
"height": "40pt",
"width": "120pt"
}
]
},
{
"type": "text",
"name": "buttonText",
"value": "Click me!",
"constraints": [
{
"type": "pos",
"anchor": "c",
"relativeAnchor": "c",
"relativeTo": "button",
"x": "0pt",
"y": "0pt"
},
{
"type": "size",
"height": "40pt",
"width": "120pt"
}
]
}
]

Using grid constraints​

This example defines a grid with 12 columns. The color layer occupies 6 columns and has a height of 240pt.

{
"name": "myGrid",
"type": "grid",
"columns": "12",
"constraints": [
{
"type": "pos",
"x": "0pt",
"y": "0pt"
},
{
"type": "size",
"height": "100%",
"width": "100%"
}
],
"children": [
{
"name": "colorArea",
"type": "color",
"value": "#2f96b4",
"constraints": [
{
"type": "grid",
"colSpan": "6",
"height": "240pt"
}
]
}
]
}

Pinning opposite corners​

This example shows how to size a layer to cover the available area. The nw anchor is positioned at the top-left corner of the screen, while the se anchor is positioned at the opposite corner.

{
"name": "colorArea",
"type": "color",
"value": "#2f96b4",
"constraints": [
{
"type": "pos",
"x": "0pt",
"y": "0pt"
},
{
"type": "pos",
"anchor": "se",
"relativeAnchor": "se",
"x": "0pt",
"y": "0pt"
}
]
}

Troubleshooting​

If a layout does not appear as expected, check the following:

  1. Are the layers listed in the correct order?
  2. Are the constraints configured correctly?
  3. Do anchor, relativeAnchor, and relativeTo reference the intended points and layers?
  4. If a layer is inside a grid, is its colSpan valid, and is only one vertical sizing attribute specified?