EnvironmentValues
A collection of environment values propagated through a view hierarchy.
Declaration
struct EnvironmentValuesOverview
SwiftUI exposes a collection of values to your app’s views in an EnvironmentValues structure. To read a value from the structure, declare a property using the Environment property wrapper and specify the value’s key path. For example, you can read the current locale:
@Environment(\.locale) var locale: LocaleUse the property you declare to dynamically control a view’s layout. SwiftUI automatically sets or updates many environment values, like pixelLength, scenePhase, or locale, based on device characteristics, system state, or user settings. For others, like lineLimit, SwiftUI provides a reasonable default value.
You can set or override some values using the environment(_:_:) view modifier:
MyView()
.environment(\.lineLimit, 2)The value that you set affects the environment for the view that you modify — including its descendants in the view hierarchy — but only up to the point where you apply a different environment modifier.
SwiftUI provides dedicated view modifiers for setting some values, which typically makes your code easier to read. For example, rather than setting the lineLimit value directly, as in the previous example, you should instead use the lineLimit(_:) modifier:
MyView()
.lineLimit(2)In some cases, using a dedicated view modifier provides additional functionality. For example, you must use the preferredColorScheme(_:) modifier rather than setting colorScheme directly to ensure that the new value propagates up to the presenting container when presenting a view like a popover:
MyView()
.popover(isPresented: $isPopped) {
PopoverContent()
.preferredColorScheme(.dark)
}Create a custom environment value by declaring a new property in an extension to the environment values structure and applying the Entry() macro to the variable declaration:
extension EnvironmentValues {
@Entry var myCustomValue: String = "Default value"
}
extension View {
func myCustomValue(_ myCustomValue: String) -> some View {
environment(\.myCustomValue, myCustomValue)
}
}Clients of your value then access the value in the usual way, reading it with the Environment property wrapper, and setting it with the myCustomValue view modifier.
Topics
Creating and accessing values
Accessibility
accessibilityAssistiveAccessEnabledaccessibilityDimFlashingLightsaccessibilityDifferentiateWithoutColoraccessibilityEnabledaccessibilityInvertColorsaccessibilityLargeContentViewerEnabledaccessibilityPlayAnimatedImagesaccessibilityPrefersHeadAnchorAlternativeaccessibilityQuickActionsEnabledaccessibilityReduceMotionaccessibilityReduceTransparencyaccessibilityShowButtonShapesaccessibilitySwitchControlEnabledaccessibilityVoiceOverEnabledlegibilityWeight
Actions
dismissdismissSearchdismissWindowopenImmersiveSpacedismissImmersiveSpacenewDocumentopenDocumentopenURLopenWindowpushWindowpurchaserefreshrenameresetFocusopenSettings
Authentication
Controls and input
buttonRepeatBehaviorcontrolSizedefaultWheelPickerItemHeightkeyboardShortcutmenuIndicatorVisibilitymenuOrdersearchSuggestionsPlacementpreferredPencilDoubleTapActionpreferredPencilSqueezeAction
Display characteristics
appearsActivecolorSchemecolorSchemeContrastdisplayScalehorizontalSizeClassimageScalepixelLengthsidebarRowSizeverticalSizeClassimmersiveSpaceDisplacementlabelsVisibilitymaterialActiveAppearanceTabBarPlacementtoolbarLabelStyle
Global objects
Scrolling
isScrollEnabledhorizontalScrollIndicatorVisibilityverticalScrollIndicatorVisibilityscrollDismissesKeyboardModehorizontalScrollBounceBehaviorverticalScrollBounceBehavior
State
editModeisActivityFullscreenisEnabledisFocusedisFocusEffectEnabledisHoverEffectEnabledisLuminanceReducedisPresentedisSceneCapturedisSearchingisTabBarShowingSectionsscenePhasesupportsMultipleWindows
StoreKit configuration
Text styles
allowsTighteningautocorrectionDisableddynamicTypeSizefontlayoutDirectionlineLimitlineSpacingminimumScaleFactormultilineTextAlignmenttextCasetruncationModetextSelectionAffinity
View attributes
allowedDynamicRangebackgroundMaterialbackgroundProminencebackgroundStylebadgeProminencecontentTransitioncontentTransitionAddsDrawingGroupdefaultMinListHeaderHeightdefaultMinListRowHeightheaderProminencephysicalMetricsrealityKitScenerealityViewCameraControlsredactionReasonsspringLoadingBehaviorsymbolRenderingModesymbolVariantsworldTrackingLimitations
Widgets
Deprecated environment values
disableAutocorrectionsizeCategorypresentationModePresentationModecomplicationRenderingModecontrolActiveState
Instance Properties
accessibilityReduceHighlightingEffectsaccessibilityShowBordersactivityFamilyaskPermissionbuttonSizingcredentialDataManagercredentialExportManagercredentialImportManagerdevicePickerSupportsfindContextfontResolutionContextimagePlaygroundAllowedGenerationStylesimagePlaygroundOptionsimagePlaygroundPersonalizationPolicyimagePlaygroundSelectedGenerationStyleisActivityUpdateReducedisUserAuthenticationEnabledlabelIconToTitleSpacinglabelReservedIconWidthlevelOfDetaillineHeightnavigationLinkIndicatorVisibilityremoteDeviceIdentifierrequestAgeRangerequestAppDeletionshowSignificantUpdateAcknowledgmentsupportedActivityFamiliessupportsImagePlaygroundsupportsRemoteScenessurfaceSnappingInfosymbolColorRenderingModesymbolVariableValueModetabBarPlacementtabViewBottomAccessoryPlacementwindowClippingMarginswritingToolsBehavior