Interactive previews iPhone
ViewThatFits
A view that adapts to the available space by providing the first child view that fits.
Platform Compatibility
iOS 16.0+ iPadOS 16.0+ macOS 13.0+ watchOS 9.0+ tvOS 16.0+ visionOS 1.0+
Tags
SwiftUI Code
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 allExplore Other Components
Browse allWheelDatePickerStyle
A date picker style that displays each component as columns in a scrollable wheel
ToolbarItem Shared Background Visibility
Controls the visibility of the glass background effect on items in the toolbar. In certain contexts, such as the navigation bar on iOS and the window toolbar on macOS, toolbar items will be given a glass background effect that is shared with other items in the same logical grouping
Toolbar Overflow Menu
The overflow menu of a toolbar. An overflow menu represents actions that are always placed in the toolbar’s overflow menu, regardless of the toolbar mode, platform, or customizability.