Declaration
import { ViewThatFits } from 'swiftui-react-native';@frozen nonisolated struct ViewThatFits<Content> where Content : ViewInitializers
One SwiftUI initializer backs <ViewThatFits>.
<ViewThatFits
in?: SwiftUI.AxisSet
>
children?: ReactNode
</ViewThatFits>nonisolated init(
in axes: Axis.Set = [.horizontal, .vertical],
@ContentBuilder content: () -> Content
)A set of axes to constrain children to. The set may contain
Axis/horizontal,Axis/vertical, or both of these.ViewThatFitschooses the first child whose size fits within the proposed size on these axes. Ifaxesis an empty set,ViewThatFitsuses the first child view. By default,ViewThatFitsuses both axes.Default
`[.horizontal, .vertical]`Swiftin axes: Axis.SetchildrenReactNodeContent. Strings become
Text; React Native views are laid out by Yoga and placed by SwiftUI.Swift
content: () -> Content
Also takes the common view props: modifiers, style and testID.
Overview
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
HStackthat contains aTextview and aProgressView. - 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)
}From Apple’s SwiftUI documentation. Code samples there are Swift.
Relationships
Conforms to
- View
See also
More in Views.
- ArrangementViewA view that arranges primary and secondary content using an adaptive layout that responds to the environment.
- ControlGroupA container view that displays semantically-related controls in a visually-appropriate manner for the context
- DividerA visual element that can be used to separate other content.
- EditButtonA button that toggles the edit mode environment value.
- EmptyViewA view that doesn't contain any content.
- GaugeA view that shows a value within a range.
- GroupA type that collects multiple instances of a content type --- like views, scenes, or commands --- into a single unit.
- GroupBoxA stylized view, with an optional label, that visually collects a logical grouping of content.
- RenameButtonA button that triggers a standard rename action.
- SwipeActionsSwipe actions of a list row (SwiftUI swipeActions(edge:allowsFullSwipe:content:)). Place it inside a List.
- TimelineViewA view that updates according to a schedule that you provide.