ViewThatFits
A view that adapts to the available space by providing the first child view that fits.
Declaration
@frozen struct ViewThatFits<Content> where Content : ViewOverview
ViewThatFits evaluates its child views in the order you provide them to the initializer. It selects the first child whose ideal size on the constrained axes fits within the proposed size. This means that you provide views in order of preference. Usually this order is largest to smallest, but since a view might fit along one constrained axis but not the other, this isn’t always the case. By default, ViewThatFits constrains in both the horizontal and vertical axes.
The following example shows an UploadProgressView that uses ViewThatFits to display the upload progress in one of three ways. In order, it attempts to display:
An HStack that contains a Text view and a ProgressView.
Only the
ProgressView.Only the
Textview.
The progress views are fixed to a 100-point width.
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))")
}
}
}This use of ViewThatFits evaluates sizes only on the horizontal axis. The following code fits the UploadProgressView to several fixed widths:
VStack {
UploadProgressView(uploadProgress: 0.75)
.frame(maxWidth: 200)
UploadProgressView(uploadProgress: 0.75)
.frame(maxWidth: 100)
UploadProgressView(uploadProgress: 0.75)
.frame(maxWidth: 50)
}[Image]