UIEnvironment - A framework that mimics the SwiftUI view's environment to replicate the value distribution thought your UIKit app.

Overview

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 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:

override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = userInterfaceStyle == .dark ? .black : .white
}

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
    }
}

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 the UIEnvironmentValues structure. For information about creating custom environment values, see the UIEnvironmentKey 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.

  1. From the File menu, select Add Packages...
  2. Enter "https://github.com/nonameplum/uienvironment" into the package repository URL text field
  3. Depending on how your project is structured:
    • 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:

.package(url: "https://github.com/nonameplum/uienvironment"),

Include "UIEnvironment" as a dependency for your executable target:

", dependencies: [ .product(name: "UIEnvironment", package: "uienvironment"), ]),">
.target(name: "", dependencies: [
    .product(name: "UIEnvironment", package: "uienvironment"),
]),

Finally, add import UIEnvironment to your source code.

License

This library is released under the MIT license. See LICENSE for details.

You might also like...
SandboxKit - Framework that makes it easy to launch a single Scene of your application

SandboxKit This framework makes debugging more efficient in your application. Sandbox is the name of a structure that improves the efficiency of debug

Displays your HomeKit temperature sensors in your menu bar
Displays your HomeKit temperature sensors in your menu bar

Temperature Glance Displays your HomeKit temperature sensors in your menu bar Screenshot Note This is a very simple app that I made for myself but dec

Save development time! Respresso automatically transforms and delivers your digital assets into your projects
Save development time! Respresso automatically transforms and delivers your digital assets into your projects

Introduction Respresso is a centralized resource manager for shared Android, iOS and Web frontend projects. It allows you to simply import the latest

Save development time! Respresso automatically transforms and delivers your digital assets into your projects
Save development time! Respresso automatically transforms and delivers your digital assets into your projects

Respresso Android client Respresso is a centralized resource manager for shared Android, iOS and Web frontend projects. It allows you to simply import

Save development time! Respresso automatically transforms and delivers your digital assets into your projects
Save development time! Respresso automatically transforms and delivers your digital assets into your projects

Respresso iOS client Respresso is a centralized resource manager for shared Android, iOS and Web frontend projects. It allows you to simply import the

You can monitor your APIs and websites on your menubar. Gives you status code 🎉 Cool & good
You can monitor your APIs and websites on your menubar. Gives you status code 🎉 Cool & good

Hope not. Monitor your APIs and websites on your menubar. For macOS. Right now! YyeeeHav!

How to develop an iOS 14 application with SwiftUI 2.0 framework. How to create an Onboarding Screen with Page Tab View

Ama-Fruits USER INTERFACE AND USER EXPERIENCE APP DESIGN How to develop an iOS 14 application with SwiftUI 2.0 framework. How to create an Onboarding

Create an easy to peek SwiftUI View to showcase your own data, catalog, images, or anything you'd like.
Create an easy to peek SwiftUI View to showcase your own data, catalog, images, or anything you'd like.

Create an easy to peek SwiftUI View to showcase your own data, catalog, images, or anything you'd like.

Turn your Swift data model into a working CRUD app.
Turn your Swift data model into a working CRUD app.

Model2App is a simple library that lets you quickly generate a CRUD iOS app based on just a data model defined in Swift. (CRUD - Create Read Update De

Releases(1.0.0)
Owner
Łukasz Śliwiński
Łukasz Śliwiński
Easy-to-use segues in SwiftUI, allowing for presenting views using common UIKIt Segue types - push, modal and popover

Easy-to-use segues in SwiftUI, allowing for presenting views using common UIKIt Segue types - push, modal and popover

Gordan Glavaš 5 Apr 16, 2022
Pendo captures product usage data, gathers user feedback, and lets you communicate in-app to onboard, educate, and guide users to value

Pendo SDK for IOS The Pendo Mobile SDK is a code-less, retro-active analytics collector across all of your app's versions. The SDK also allows present

Pendo 30 Jan 4, 2023
Goal: Create an iOS app with two views, MainViewController and DetailViewController

Goal: Create an iOS app with two views, MainViewController and DetailViewController

Jesse Rae 0 Jan 23, 2022
A collection of common SwiftUI and UIKit utilities.

KippleUI A collection of common SwiftUI and UIKit utilities. ⚠️ The code in this library has been made public as-is for the purposes of education, dis

Kipple 10 Sep 2, 2022
UIKit-based app project template for Swift Playgrounds 4

playgrounds-uikit-app This is a simplistic sample template for Swift Playgrounds 4 to begin with a UIKit-based app delegate & window scene instead of

Steven Troughton-Smith 39 Sep 19, 2022
Mimicrated views and controls to native Apple appearance.

Mimicrated views and controls to native Apple appearance. If you have any ideas of what elements can be added, let me know. Below you will see previews of all the elements and how to use them.

Ivan Vorobei 85 Dec 3, 2022
XCode Preview template for UIkit based project.

SwiftPreview XCode Preview template for UIkit based project. Support custom file template class inherit from UIView and UIViewController. How to use?

Won Heo 10 Mar 31, 2022
The Art World in Your Pocket or Your Trendy Tech Company's Tote, Artsy's mobile app.

Meta State: production Point People: Brian Beckerle, Mounir Dhahri, Pavlos Vinieratos CI : This is an Artsy OSS project. Other mobile projects are Ene

Artsy 3.2k Jan 4, 2023
Start your next Open-Source Swift Framework 📦

SwiftKit enables you to easily generate a cross platform Swift Framework from your command line. It is the best way to start your next Open-Source Swi

Sven Tiigi 821 Dec 28, 2022
ConfettiKit is a custom framework used to add Confetti on your iOS/iPadOS projects.

ConfettiKit is a custom framework used to add Confetti on your iOS/iPadOS projects. The kit provides variety of customisations inorder to design a confetti which matches your project's UI. ConfettiKit makes your work of adding Confetti on your project with just one line of code.

Gokul Nair 14 Sep 27, 2022