History
Loading...
Loading...
August 28, 2025
@Environment(\.dismiss) instead of @Environment(\.presentationMode) for iOS 15+. The new dismiss action is more concise and handles both modal and navigation dismissals automatically with just dismiss().Custom environment values provide a clean way to pass configuration data down the view hierarchy without prop drilling. They automatically propagate to all child views and can be overridden at any level.
// Define custom environment key
struct ThemeKey: EnvironmentKey {
static let defaultValue = Theme.light
}
extension EnvironmentValues {
var theme: Theme {
get { self[ThemeKey.self] }
set { self[ThemeKey.self] = newValue }
}
}
// Usage in parent view
ContentView()
.environment(\.theme, userSelectedTheme)
// Access in child views
struct ChildView: View {
@Environment(\.theme) private var theme
var body: some View {
Text("Hello")
.foregroundColor(theme.primaryColor)
}
}This pattern eliminates the need to pass data through multiple view initializers, reduces coupling between views, and makes it easy to provide different configurations for different parts of your app while maintaining type safety.