# Layouts

When we create apps, we don’t just care about creating our views — we also want to lay them out. In SwiftUI, there are three main ways to lay out our views:

1. `HStack`
2. `VStack`
3. `ZStack`

### HStack

An `HStack` is a view that arranges its views in a **horizontal** line. For example, to lay out the text “Cornell” and “AppDev” horizontal, we can use the following code:

```swift
HStack {
    Text("Cornell")
    Text("AppDev")
}
```

<figure><img src="/files/65czR9FkbXPLcmhB8KFT" alt="" width="237"><figcaption><p>HStack</p></figcaption></figure>

### VStack

A `VStack` is a view that arranges its views in a *vertical* line. For example, to lay out the text “Cornell” and “AppDev” vertically, we can use the following code:

```swift
VStack {
    Text("Cornell")
    Text("AppDev")
}
```

<figure><img src="/files/BDe8u3neel832aztHIGA" alt="" width="144"><figcaption><p>VStack</p></figcaption></figure>

### ZStack

A `ZStack` is a view that *overlays* its subviews, aligning them in both axes. For example, to place the text “AppDev” above a background color of red, we can use the following code:

```swift
ZStack {
    Color.red
    Text("AppDev")
}
```

<figure><img src="/files/AeZUk2u4YSKpLIVtEk9h" alt="" width="214"><figcaption><p>ZStack</p></figcaption></figure>

### Spacers

A `Spacer` is a flexible view that expands along the major axis of its containing stack layout, or on both axes if not contained in a stack. For example, to place the text “Cornell” as far away from “AppDev” in an `HStack`, we can use the following code:

```swift
HStack {
    Text("Cornell")
    Spacer()
    Text("AppDev")
}
```

<figure><img src="/files/zNjRshYtP3GcfiSvAgJM" alt="" width="375"><figcaption><p>Spacer in an HStack</p></figcaption></figure>

### Other Views

Of course, there are other types of views we can use to lay out our views. I recommend reading the Apple Documentation for each one if you’re interested.

* LazyHStack
* LazyVStack
* List
* ScrollView
* LazyHGrid
* LazyVGrid
* Form
* Divider


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ios-course.cornellappdev.com/resources/textbook/swiftui/layouts.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
