{"abstract":[{"text":"A view that adapts to the available space by providing the first","type":"text"},{"text":" ","type":"text"},{"text":"child view that fits.","type":"text"}],"hierarchy":{"paths":[["doc:\/\/com.apple.documentation\/documentation\/technologies","doc:\/\/com.apple.SwiftUI\/documentation\/SwiftUI"]]},"identifier":{"interfaceLanguage":"swift","url":"doc:\/\/com.apple.SwiftUI\/documentation\/SwiftUI\/ViewThatFits"},"kind":"symbol","legalNotices":{"copyright":"Copyright &copy; 2026 Apple Inc. All rights reserved.","privacyPolicy":"https:\/\/www.apple.com\/privacy\/privacy-policy","termsOfUse":"https:\/\/www.apple.com\/legal\/internet-services\/terms\/site.html"},"metadata":{"availableLanguages":["de-DE","en-US","es-419","fr-FR","it-IT","ja-JP","ko-KR","pt-BR","zh-CN"],"externalID":"s:7SwiftUI12ViewThatFitsV","fragments":[{"kind":"keyword","text":"struct"},{"kind":"text","text":" "},{"kind":"identifier","text":"ViewThatFits"}],"modules":[{"name":"SwiftUI"}],"navigatorTitle":[{"kind":"identifier","text":"ViewThatFits"}],"platforms":[{"introducedAt":"16.0","name":"iOS"},{"introducedAt":"16.0","name":"iPadOS"},{"introducedAt":"16.0","name":"Mac Catalyst"},{"introducedAt":"13.0","name":"macOS"},{"introducedAt":"16.0","name":"tvOS"},{"introducedAt":"1.0","name":"visionOS"},{"introducedAt":"9.0","name":"watchOS"}],"role":"symbol","roleHeading":"Structure","symbolKind":"struct","title":"ViewThatFits"},"primaryContentSections":[{"declarations":[{"languages":["swift"],"platforms":["iOS","iPadOS","Mac Catalyst","macOS","tvOS","visionOS","watchOS"],"tokens":[{"kind":"attribute","text":"@frozen"},{"kind":"text","text":" "},{"kind":"attribute","text":"nonisolated"},{"kind":"text","text":" "},{"kind":"keyword","text":"struct"},{"kind":"text","text":" "},{"kind":"identifier","text":"ViewThatFits"},{"kind":"text","text":"<"},{"kind":"genericParameter","text":"Content"},{"kind":"text","text":"> "},{"kind":"keyword","text":"where"},{"kind":"text","text":" "},{"kind":"typeIdentifier","text":"Content"},{"kind":"text","text":" : "},{"identifier":"doc:\/\/com.apple.SwiftUI\/documentation\/SwiftUI\/View","kind":"typeIdentifier","preciseIdentifier":"s:7SwiftUI4ViewP","text":"View"}]}],"kind":"declarations"},{"content":[{"anchor":"overview","level":2,"text":"Overview","type":"heading"},{"inlineContent":[{"code":"ViewThatFits","type":"codeVoice"},{"text":" evaluates its child views in the order you provide them","type":"text"},{"text":" ","type":"text"},{"text":"to the initializer. It selects the first child whose ideal size on the","type":"text"},{"text":" ","type":"text"},{"text":"constrained axes fits within the proposed size. This means that you","type":"text"},{"text":" ","type":"text"},{"text":"provide views in order of preference. Usually this order is largest to","type":"text"},{"text":" ","type":"text"},{"text":"smallest, but since a view might fit along one constrained axis but not the","type":"text"},{"text":" ","type":"text"},{"text":"other, this isn’t always the case. By default, ","type":"text"},{"code":"ViewThatFits","type":"codeVoice"},{"text":" constrains","type":"text"},{"text":" ","type":"text"},{"text":"in both the horizontal and vertical axes.","type":"text"}],"type":"paragraph"},{"inlineContent":[{"text":"The following example shows an ","type":"text"},{"code":"UploadProgressView","type":"codeVoice"},{"text":" that uses ","type":"text"},{"code":"ViewThatFits","type":"codeVoice"},{"text":" ","type":"text"},{"text":"to display the upload progress in one of three ways. In order, it attempts","type":"text"},{"text":" ","type":"text"},{"text":"to display:","type":"text"}],"type":"paragraph"},{"items":[{"content":[{"inlineContent":[{"text":"An ","type":"text"},{"identifier":"doc:\/\/com.apple.SwiftUI\/documentation\/SwiftUI\/HStack","isActive":true,"type":"reference"},{"text":" that contains a ","type":"text"},{"identifier":"doc:\/\/com.apple.SwiftUI\/documentation\/SwiftUI\/Text","isActive":true,"type":"reference"},{"text":" view and a ","type":"text"},{"identifier":"doc:\/\/com.apple.SwiftUI\/documentation\/SwiftUI\/ProgressView","isActive":true,"type":"reference"},{"text":".","type":"text"}],"type":"paragraph"}]},{"content":[{"inlineContent":[{"text":"Only the ","type":"text"},{"code":"ProgressView","type":"codeVoice"},{"text":".","type":"text"}],"type":"paragraph"}]},{"content":[{"inlineContent":[{"text":"Only the ","type":"text"},{"code":"Text","type":"codeVoice"},{"text":" view.","type":"text"}],"type":"paragraph"}]}],"type":"unorderedList"},{"inlineContent":[{"text":"The progress views are fixed to a 100-point width.","type":"text"}],"type":"paragraph"},{"code":["struct UploadProgressView: View {","    var uploadProgress: Double","","    var body: some View {","        ViewThatFits(in: .horizontal) {","            HStack {","                Text(\"\\(uploadProgress.formatted(.percent))\")","                ProgressView(value: uploadProgress)","                    .frame(width: 100)","            }","            ProgressView(value: uploadProgress)","                .frame(width: 100)","            Text(\"\\(uploadProgress.formatted(.percent))\")","        }","    }","}"],"copyToClipboard":true,"lineAnnotations":[],"showLineNumbers":false,"syntax":"swift","type":"codeListing","wrap":0},{"inlineContent":[{"text":"This use of ","type":"text"},{"code":"ViewThatFits","type":"codeVoice"},{"text":" evaluates sizes only on the horizontal axis. The","type":"text"},{"text":" ","type":"text"},{"text":"following code fits the ","type":"text"},{"code":"UploadProgressView","type":"codeVoice"},{"text":" to several fixed widths:","type":"text"}],"type":"paragraph"},{"code":["VStack {","    UploadProgressView(uploadProgress: 0.75)","        .frame(maxWidth: 200)","    UploadProgressView(uploadProgress: 0.75)","        .frame(maxWidth: 100)","    UploadProgressView(uploadProgress: 0.75)","        .frame(maxWidth: 50)","}"],"copyToClipboard":true,"lineAnnotations":[],"showLineNumbers":false,"syntax":"swift","type":"codeListing","wrap":0},{"inlineContent":[{"identifier":"ViewThatFits-1","type":"image"}],"type":"paragraph"}],"kind":"content"}],"relationshipsSections":[{"identifiers":["doc:\/\/com.apple.SwiftUI\/documentation\/SwiftUI\/View"],"kind":"relationships","title":"Conforms To","type":"conformsTo"}],"schemaVersion":{"major":0,"minor":3,"patch":0},"sections":[],"topicSections":[{"anchor":"Creating-a-view-that-fits","identifiers":["doc:\/\/com.apple.SwiftUI\/documentation\/SwiftUI\/ViewThatFits\/init(in:content:)"],"title":"Creating a view that fits"}],"variants":[{"paths":["\/documentation\/swiftui\/viewthatfits"],"traits":[{"interfaceLanguage":"swift"}]}],"references":{"SwiftUI-PageImage-card.png":{"alt":"A stylized Swift logo using dashes on a white or blue background.","identifier":"SwiftUI-PageImage-card.png","type":"image","variants":[{"traits":["2x","light"],"url":"\/images\/com.apple.SwiftUI\/SwiftUI-PageImage-card@2x.png"},{"traits":["2x","dark"],"url":"\/images\/com.apple.SwiftUI\/SwiftUI-PageImage-card~dark@2x.png"}]},"ViewThatFits-1":{"alt":"A vertical stack showing three expressions of progress, constrained by","identifier":"ViewThatFits-1","type":"image","variants":[{"traits":["2x","light"],"url":"\/images\/com.apple.SwiftUI\/ViewThatFits-1@2x.png"},{"traits":["2x","dark"],"url":"\/images\/com.apple.SwiftUI\/ViewThatFits-1~dark@2x.png"}]},"doc://com.apple.SwiftUI/documentation/SwiftUI":{"abstract":[{"text":"Declare the user interface and behavior for your app on every platform.","type":"text"}],"identifier":"doc:\/\/com.apple.SwiftUI\/documentation\/SwiftUI","images":[{"identifier":"SwiftUI-PageImage-card.png","type":"card"}],"kind":"symbol","role":"collection","title":"SwiftUI","type":"topic","url":"\/documentation\/swiftui"},"doc://com.apple.SwiftUI/documentation/SwiftUI/HStack":{"abstract":[{"text":"A view that arranges its subviews in a horizontal line.","type":"text"}],"fragments":[{"kind":"keyword","text":"struct"},{"kind":"text","text":" "},{"kind":"identifier","text":"HStack"}],"identifier":"doc:\/\/com.apple.SwiftUI\/documentation\/SwiftUI\/HStack","kind":"symbol","navigatorTitle":[{"kind":"identifier","text":"HStack"}],"role":"symbol","title":"HStack","type":"topic","url":"\/documentation\/swiftui\/hstack"},"doc://com.apple.SwiftUI/documentation/SwiftUI/ProgressView":{"abstract":[{"text":"A view that shows the progress toward completion of a task.","type":"text"}],"fragments":[{"kind":"keyword","text":"struct"},{"kind":"text","text":" "},{"kind":"identifier","text":"ProgressView"}],"identifier":"doc:\/\/com.apple.SwiftUI\/documentation\/SwiftUI\/ProgressView","kind":"symbol","navigatorTitle":[{"kind":"identifier","text":"ProgressView"}],"role":"symbol","title":"ProgressView","type":"topic","url":"\/documentation\/swiftui\/progressview"},"doc://com.apple.SwiftUI/documentation/SwiftUI/Text":{"abstract":[{"text":"A view that displays one or more lines of read-only text.","type":"text"}],"fragments":[{"kind":"keyword","text":"struct"},{"kind":"text","text":" "},{"kind":"identifier","text":"Text"}],"identifier":"doc:\/\/com.apple.SwiftUI\/documentation\/SwiftUI\/Text","kind":"symbol","navigatorTitle":[{"kind":"identifier","text":"Text"}],"role":"symbol","title":"Text","type":"topic","url":"\/documentation\/swiftui\/text"},"doc://com.apple.SwiftUI/documentation/SwiftUI/View":{"abstract":[{"text":"A type that represents part of your app’s user interface and provides","type":"text"},{"text":" ","type":"text"},{"text":"modifiers that you use to configure views.","type":"text"}],"fragments":[{"kind":"keyword","text":"protocol"},{"kind":"text","text":" "},{"kind":"identifier","text":"View"}],"identifier":"doc:\/\/com.apple.SwiftUI\/documentation\/SwiftUI\/View","kind":"symbol","navigatorTitle":[{"kind":"identifier","text":"View"}],"role":"symbol","title":"View","type":"topic","url":"\/documentation\/swiftui\/view"},"doc://com.apple.SwiftUI/documentation/SwiftUI/ViewThatFits":{"abstract":[{"text":"A view that adapts to the available space by providing the first","type":"text"},{"text":" ","type":"text"},{"text":"child view that fits.","type":"text"}],"fragments":[{"kind":"keyword","text":"struct"},{"kind":"text","text":" "},{"kind":"identifier","text":"ViewThatFits"}],"identifier":"doc:\/\/com.apple.SwiftUI\/documentation\/SwiftUI\/ViewThatFits","kind":"symbol","navigatorTitle":[{"kind":"identifier","text":"ViewThatFits"}],"role":"symbol","title":"ViewThatFits","type":"topic","url":"\/documentation\/swiftui\/viewthatfits"},"doc://com.apple.SwiftUI/documentation/SwiftUI/ViewThatFits/init(in:content:)":{"abstract":[{"text":"Produces a view constrained in the given axes from one of several","type":"text"},{"text":" ","type":"text"},{"text":"alternatives provided by a content builder.","type":"text"}],"fragments":[{"kind":"identifier","text":"init"},{"kind":"text","text":"("},{"kind":"externalParam","text":"in"},{"kind":"text","text":": "},{"kind":"typeIdentifier","preciseIdentifier":"s:7SwiftUI4AxisO","text":"Axis"},{"kind":"text","text":"."},{"kind":"typeIdentifier","preciseIdentifier":"s:7SwiftUI4AxisO3SetV","text":"Set"},{"kind":"text","text":", "},{"kind":"externalParam","text":"content"},{"kind":"text","text":": () -> "},{"kind":"typeIdentifier","text":"Content"},{"kind":"text","text":")"}],"identifier":"doc:\/\/com.apple.SwiftUI\/documentation\/SwiftUI\/ViewThatFits\/init(in:content:)","kind":"symbol","role":"symbol","title":"init(in:content:)","type":"topic","url":"\/documentation\/swiftui\/viewthatfits\/init(in:content:)"},"doc://com.apple.documentation/documentation/technologies":{"abstract":[],"identifier":"doc:\/\/com.apple.documentation\/documentation\/technologies","kind":"technologies","role":"overview","title":"Technologies","type":"topic","url":"\/documentation\/technologies"}}}