Use the UIEnvironment property wrapper to read a value stored in a view’s environment. Indicate the value to read using an UIEnvironmentValues key path in the property declaration.
For example, you can create a property that reads the user interface style of the current view using the key path of the userInterfaceStyle property:
final class ViewController: UIViewController {
@UIEnvironment(\.userInterfaceStyle) private var userInterfaceStyle
...
}
You can condition a view’s content on the associated value, which
you read from the declared property by directly referring from it:
If the value changes, UIEnvironment framework updates any view
that implements UIEnvironmentUpdating.
For example, that might happen in the above example if the user
changes the Appearance settings.
final class ViewController: UIViewController {
@UIEnvironment(\.userInterfaceStyle) private var userInterfaceStyle
...
}
extension ViewController: UIEnvironmentUpdating {
func updateEnvironment() {
view.backgroundColor = userInterfaceStyle == .dark ? .black : .white
}
}
You can use this property wrapper to read but not set an environment
value. UIEnvironment framework updates some environment values automatically based on system
settings and provides reasonable defaults for others. You can override some
of these, as well as set custom environment values that you define,
using the UIEnvironmentable.environment(_:_:) convenience method.
For the complete list of environment values provided by UIEnvironment framework, see the
properties of the UIEnvironmentValues structure. For information about
creating custom environment values, see the UIEnvironmentKey protocol.
Documentation
The documentation for the latest release is available here:
If you have a single application target that needs access to the library, then add UIEnvironment directly to your application.
If you want to use this library from multiple Xcode targets, or mixing Xcode targets and SPM targets, you must create a shared framework that depends on UIEnvironment and then depend on that framework in all of your targets.
You can add UIEnvironment to an Xcode project by adding it as a package dependency.
Adding UIEnvironment as a Dependency
To use the UIEnvironment framework in a SwiftPM project, add the following line to the dependencies in your Package.swift file:
UIEnvironment
A framework that mimics the SwiftUI view’s environment to replicate the value distribution thought your UIKit view hierarchy.
Overview
Use the
UIEnvironment
property wrapper to read a value stored in a view’s environment. Indicate the value to read using anUIEnvironmentValues
key path in the property declaration. For example, you can create a property that reads the user interface style of the current view using the key path of theuserInterfaceStyle
property:You can condition a view’s content on the associated value, which you read from the declared property by directly referring from it:
If the value changes, UIEnvironment framework updates any view that implements
UIEnvironmentUpdating
. For example, that might happen in the above example if the user changes the Appearance settings.Please refer to the example application for more details.
You can use this property wrapper to read but not set an environment value. UIEnvironment framework updates some environment values automatically based on system settings and provides reasonable defaults for others. You can override some of these, as well as set custom environment values that you define, using the
UIEnvironmentable.environment(_:_:)
convenience method. For the complete list of environment values provided by UIEnvironment framework, see the properties of theUIEnvironmentValues
structure. For information about creating custom environment values, see theUIEnvironmentKey
protocol.Documentation
The documentation for the latest release is available here:
Installation
You can add UIEnvironment to an Xcode project by adding it as a package dependency.
You can add
UIEnvironment
to an Xcode project by adding it as a package dependency.Adding UIEnvironment as a Dependency
To use the UIEnvironment framework in a SwiftPM project, add the following line to the dependencies in your Package.swift file:
Include
"UIEnvironment"
as a dependency for your executable target:Finally, add
import UIEnvironment
to your source code.License
This library is released under the MIT license. See LICENSE for details.