---
title: "ViewThatFits | Explore SwiftUI"
description: "A view that adapts to the available space by providing the first child view that fits. Learn how to use ViewThatFits in SwiftUI with visual code examples."
canonical_url: "https://exploreswiftui.com/library/viewthatfits/viewthatfits"
meta:
  author: "Explore SwiftUI"
  "og:description": "A view that adapts to the available space by providing the first child view that fits. Learn how to use ViewThatFits in SwiftUI with visual code examples."
  "og:image": "https://exploreswiftui.com/_og/d/c_Component%2Ctitle_ViewThatFits%2Cdescription_A%2Bview%2Bthat%2Badapts%2Bto%2Bthe%2Bavailable%2Bspace%2Bby%2Bproviding%2Bthe%2Bfirst%2Bchild%2Bview%2Bthat%2Bfits.%2Ccategory_ViewThatFits%2Cimage_~L2FwaS9pbWFnZXMvNS92YXJpYW50cy8xP3Y9MTc5MDA3MzQ3ODg0MiZyPTImdz0xMjgwJnE9ODUmZj1qcGVn%2CimageMode_crop%2CimagePosition_center%2CimageFrame_~cG9zaXRpb246IGFic29sdXRlOyByaWdodDogMDsgd2lkdGg6IDY0MHB4OyBoZWlnaHQ6IDQ4MHB4OyBkaXNwbGF5OiBmbGV4OyBvdmVyZmxvdzogaGlkZGVuOyBiYWNrZ3JvdW5kOiAjRkZGRkZGOyBib3gtc2hhZG93OiAwIC0xMHB4IDUwcHggcmdiYSgzNyw5OSwyMzUsMC4xOCk7IGJvcmRlci1sZWZ0OiAxcHggc29saWQgcmdiYSgwLDAsMCwwLjA2KTsgdG9wOiA3NXB4OyBib3JkZXItdG9wOiAxcHggc29saWQgcmdiYSgwLDAsMCwwLjA2KTsgYm9yZGVyLWJvdHRvbTogMXB4IHNvbGlkIHJnYmEoMCwwLDAsMC4wNik7IGJvcmRlci10b3AtbGVmdC1yYWRpdXM6IDMycHg7IGJvcmRlci1ib3R0b20tbGVmdC1yYWRpdXM6IDMycHg7%2Cprops_eyJwbGF0Zm9ybXMiOlsiaU9TIiwiaVBhZE9TIiwibWFjT1MiLCJ3YXRjaE9TIiwidHZPUyIsInZpc2lvbk9TIl19%2Cp_Ii9saWJyYXJ5L3ZpZXd0aGF0Zml0cy92aWV3dGhhdGZpdHMi%2Cs_zFemX2pKZ9ZcIvlT.png"
  "og:image:height": "630"
  "og:image:type": image/png
  "og:image:width": "1200"
  "og:title": "ViewThatFits | Explore SwiftUI"
  "twitter:description": "A view that adapts to the available space by providing the first child view that fits. Learn how to use ViewThatFits in SwiftUI with visual code examples."
  "twitter:image": "https://exploreswiftui.com/_og/d/c_Component%2Ctitle_ViewThatFits%2Cdescription_A%2Bview%2Bthat%2Badapts%2Bto%2Bthe%2Bavailable%2Bspace%2Bby%2Bproviding%2Bthe%2Bfirst%2Bchild%2Bview%2Bthat%2Bfits.%2Ccategory_ViewThatFits%2Cimage_~L2FwaS9pbWFnZXMvNS92YXJpYW50cy8xP3Y9MTc5MDA3MzQ3ODg0MiZyPTImdz0xMjgwJnE9ODUmZj1qcGVn%2CimageMode_crop%2CimagePosition_center%2CimageFrame_~cG9zaXRpb246IGFic29sdXRlOyByaWdodDogMDsgd2lkdGg6IDY0MHB4OyBoZWlnaHQ6IDQ4MHB4OyBkaXNwbGF5OiBmbGV4OyBvdmVyZmxvdzogaGlkZGVuOyBiYWNrZ3JvdW5kOiAjRkZGRkZGOyBib3gtc2hhZG93OiAwIC0xMHB4IDUwcHggcmdiYSgzNyw5OSwyMzUsMC4xOCk7IGJvcmRlci1sZWZ0OiAxcHggc29saWQgcmdiYSgwLDAsMCwwLjA2KTsgdG9wOiA3NXB4OyBib3JkZXItdG9wOiAxcHggc29saWQgcmdiYSgwLDAsMCwwLjA2KTsgYm9yZGVyLWJvdHRvbTogMXB4IHNvbGlkIHJnYmEoMCwwLDAsMC4wNik7IGJvcmRlci10b3AtbGVmdC1yYWRpdXM6IDMycHg7IGJvcmRlci1ib3R0b20tbGVmdC1yYWRpdXM6IDMycHg7%2Cprops_eyJwbGF0Zm9ybXMiOlsiaU9TIiwiaVBhZE9TIiwibWFjT1MiLCJ3YXRjaE9TIiwidHZPUyIsInZpc2lvbk9TIl19%2Cp_Ii9saWJyYXJ5L3ZpZXd0aGF0Zml0cy92aWV3dGhhdGZpdHMi%2Cs_zFemX2pKZ9ZcIvlT.png"
  "twitter:title": "ViewThatFits | Explore SwiftUI"
---

> Index of every page on this site: [llms.txt](https://exploreswiftui.com/llms.txt)

**Interactive previews**[ iPhone](https://exploreswiftui.com/iphone?element=5)

# **ViewThatFits**

A view that adapts to the available space by providing the first child view that fits.

![ViewThatFits - iOS on iPhone - Portrait - Flat Design - Light Mode - Preview](https://exploreswiftui.com/api/images/5/variants/1?v=1790073478842&r=2&w=800&q=80&f=webp)

### **Platform Compatibility**

iOS 16.0+ iPadOS 16.0+ macOS 13.0+ watchOS 9.0+ tvOS 16.0+ visionOS 1.0+

### **Tags**

### **SwiftUI Code**

[ Docs](https://developer.apple.com/documentation/swiftui/viewthatfits)

```
var body: some View {
  VStack(alignment: .leading) {
    testContent
      .background(.red)
      .frame(maxWidth: 300)
    testContent
      .background(.red.secondary)
      .frame(maxWidth: 200)
    testContent
      .background(.red.tertiary)
      .frame(maxWidth: 100)
    testContent
      .background(.red.quaternary)
      .frame(maxWidth: 50)
  }
}

@ViewBuilder
var testContent: some View {
  ViewThatFits {
    Text("Available width 300")
      .frame(width: 300, height: 20)
      .foregroundStyle(.white)
    Text("Available width 200")
      .frame(width: 200, height: 20)
      .foregroundStyle(.white)
    Text("A. w. 100")
      .frame(width: 100, height: 20)
      .foregroundStyle(.white)
    Text("Fallback")
      .frame(width: 80, height: 20)
      .foregroundStyle(.white)
  }
}
```

### **Found an issue or have a suggestion?**

Created: May 29, 2025Updated: May 31, 2025

### **More ViewThatFits Components **

[**View all **](https://exploreswiftui.com/library/viewthatfits)

![ViewThatFits Vertical - iOS on iPhone - Portrait - Flat Design - Light Mode - Preview](https://exploreswiftui.com/api/images/6/variants/20?v=1790073478842&r=2&w=412&q=75&f=webp)

### **ViewThatFits Vertical**

A view that adapts to the available vertical space by providing the first child view that fits.

### **Explore Other Components **

[**Browse all **](https://exploreswiftui.com/)

![Split Arrangement Layout Ratio - iOS on iPhone Duo - Landscape - Fully Open - Liquid Glass - Light Mode - Preview](https://exploreswiftui.com/api/images/244/variants/1564?v=1790273511100&r=2&w=412&q=75&f=webp)

### **Split Arrangement Layout Ratio**

Sets the preferred size ratio for an arrangement view in a split style. Use this modifier when you want to customize the size of the view compared to its other views in the split layout.

![InlinePickerStyle - iOS on iPhone - Portrait - Liquid Glass - Light Mode - Preview](https://exploreswiftui.com/api/images/179/variants/574?v=1790073478842&r=2&w=412&q=75&f=webp)

### **InlinePickerStyle**

A PickerStyle where each option is displayed inline with other views in the current container.

![UIKit Separator Colors - iOS on iPhone - Portrait - Flat Design - Light Mode - Preview](https://exploreswiftui.com/api/images/14/variants/57?v=1790073478842&r=2&w=412&q=75&f=webp)

### **UIKit Separator Colors**

The UIKit separator colors that are also used in other components, such as the TextField

![List Row Background - iOS on iPhone - Portrait - Flat Design - Light Mode - Preview](https://exploreswiftui.com/api/images/121/variants/345?v=1790073478842&r=2&w=412&q=75&f=webp)

### **List Row Background**

Places a custom background view behind a list row item.

![Preferred Top Column - iOS on iPhone - Portrait - Liquid Glass - Light Mode - Preview](https://exploreswiftui.com/api/images/256/variants/1967?v=1791379474964&r=2&w=412&q=75&f=webp)

### **Preferred Top Column**

Controls which column appears on top of the stack when NavigationSplitView collapses into a single stack in certain contexts, such as on iPhone, when iPhone Duo is closed, or on Apple Watch. By default, the sidebar appears on top. In this case, the detail column does instead.

![Gauge Colors - iOS on iPhone - Portrait - Liquid Glass - Light Mode - Preview](https://exploreswiftui.com/api/images/48/variants/261?v=1790073478842&r=2&w=412&q=75&f=webp)

### **Gauge Colors**

Customizing the colors of different styles with minimal effort

![Disabled Toolbar Vertical Behavior - iOS on iPhone Duo - Landscape - Fully Open - Liquid Glass - Light Mode - Preview](https://exploreswiftui.com/api/images/246/variants/1636?v=1790277107518&r=2&w=412&q=75&f=webp)

### **Disabled Toolbar Vertical Behavior**

Disables the vertical bar.

![ShareLink - iOS on iPhone - Portrait - Liquid Glass - Light Mode - Preview](https://exploreswiftui.com/api/images/74/variants/28?v=1790073478842&r=2&w=412&q=75&f=webp)

### **ShareLink**

A view that controls a sharing presentation

```json
{"@context":"https://schema.org","@graph":[{"@id":"https://exploreswiftui.com/#website","@type":"WebSite","description":"A visual library for SwiftUI APIs, components, and modifiers","inLanguage":"en-US","name":"Explore SwiftUI","url":"https://exploreswiftui.com/","publisher":{"@id":"https://exploreswiftui.com/#identity"}},{"@id":"https://exploreswiftui.com/library/viewthatfits/viewthatfits#webpage","@type":"WebPage","dateModified":"2025-05-31T17:50:47.220Z","datePublished":"2025-05-29T08:01:23.868Z","description":"A view that adapts to the available space by providing the first child view that fits. Learn how to use ViewThatFits in SwiftUI with visual code examples.","name":"ViewThatFits","url":"https://exploreswiftui.com/library/viewthatfits/viewthatfits","breadcrumb":{"@id":"https://exploreswiftui.com/library/viewthatfits/viewthatfits#breadcrumb"},"isPartOf":{"@id":"https://exploreswiftui.com/#website"},"mainEntity":{"@id":"https://exploreswiftui.com/library/viewthatfits/viewthatfits#article"},"potentialAction":[{"@type":"ReadAction","target":["https://exploreswiftui.com/library/viewthatfits/viewthatfits"]}],"primaryImageOfPage":{"@id":"https://exploreswiftui.com/#logo"}},{"@id":"https://exploreswiftui.com/#identity","@type":"Organization","description":"A visual library for SwiftUI APIs, components, and modifiers","name":"Explore SwiftUI","url":"https://exploreswiftui.com","logo":{"@id":"https://exploreswiftui.com/#logo"},"sameAs":["https://x.com/Typ0genius","https://mastodon.social/@Typ0genius"]},{"@id":"https://exploreswiftui.com/library/viewthatfits/viewthatfits#article","citation":"https://developer.apple.com/documentation/swiftui/viewthatfits","dateModified":"2025-05-31T17:50:47.220Z","datePublished":"2025-05-29T08:01:23.868Z","description":"A view that adapts to the available space by providing the first child view that fits. Learn how to use ViewThatFits in SwiftUI with visual code examples.","headline":"ViewThatFits","inLanguage":"en-US","name":"ViewThatFits","thumbnailUrl":"https://exploreswiftui.com/api/images/5/variants/1?v=1790073478842&r=2","url":"https://exploreswiftui.com/library/viewthatfits/viewthatfits","@type":["Article","TechArticle"],"about":{"@type":"Thing","name":"SwiftUI"},"author":{"@id":"https://exploreswiftui.com/#identity"},"hasPart":{"@id":"https://exploreswiftui.com/library/viewthatfits/viewthatfits#source-code"},"image":{"@id":"https://exploreswiftui.com/#/schema/image/1"},"isPartOf":{"@id":"https://exploreswiftui.com/library/viewthatfits/viewthatfits#webpage"},"mainEntityOfPage":{"@id":"https://exploreswiftui.com/library/viewthatfits/viewthatfits#webpage"},"publisher":{"@id":"https://exploreswiftui.com/#identity"}},{"@id":"https://exploreswiftui.com/library/viewthatfits/viewthatfits#source-code","@type":"SoftwareSourceCode","codeSampleType":"full example","description":"A view that adapts to the available space by providing the first child view that fits. Learn how to use ViewThatFits in SwiftUI with visual code examples.","name":"ViewThatFits SwiftUI Example","programmingLanguage":"Swift","runtimePlatform":"SwiftUI","text":"var body: some View {\n  VStack(alignment: .leading) {\n    testContent\n      .background(.red)\n      .frame(maxWidth: 300)\n    testContent\n      .background(.red.secondary)\n      .frame(maxWidth: 200)\n    testContent\n      .background(.red.tertiary)\n      .frame(maxWidth: 100)\n    testContent\n      .background(.red.quaternary)\n      .frame(maxWidth: 50)\n  }\n}\n\n@ViewBuilder\nvar testContent: some View {\n  ViewThatFits {\n    Text(\"Available width 300\")\n      .frame(width: 300, height: 20)\n      .foregroundStyle(.white)\n    Text(\"Available width 200\")\n      .frame(width: 200, height: 20)\n      .foregroundStyle(.white)\n    Text(\"A. w. 100\")\n      .frame(width: 100, height: 20)\n      .foregroundStyle(.white)\n    Text(\"Fallback\")\n      .frame(width: 80, height: 20)\n      .foregroundStyle(.white)\n  }\n}","about":[{"@type":"Thing","name":"SwiftUI"},{"@type":"Thing","name":"ViewThatFits"},{"@type":"Thing","name":"ViewThatFits"}]},{"@id":"https://exploreswiftui.com/library/viewthatfits/viewthatfits#breadcrumb","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","name":"Home","item":"https://exploreswiftui.com","position":1},{"@type":"ListItem","name":"ViewThatFits","item":"https://exploreswiftui.com/library/viewthatfits","position":2},{"@type":"ListItem","name":"ViewThatFits","item":"https://exploreswiftui.com/library/viewthatfits/viewthatfits","position":3}]},{"@id":"https://exploreswiftui.com/#logo","@type":"ImageObject","caption":"Explore SwiftUI","contentUrl":"https://exploreswiftui.com/apple-touch-icon.png","inLanguage":"en","url":"https://exploreswiftui.com/apple-touch-icon.png"},{"@id":"https://exploreswiftui.com/#/schema/image/1","@type":"ImageObject","contentUrl":"https://exploreswiftui.com/api/images/5/variants/1?v=1790073478842&r=2","inLanguage":"en","url":"https://exploreswiftui.com/api/images/5/variants/1?v=1790073478842&r=2"}]}
```