Coder Social home page Coder Social logo

adam-zethraeus / uienvironment Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nonameplum/uienvironment

0.0 0.0 0.0 246 KB

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

License: MIT License

Swift 100.00%

uienvironment's Introduction

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:

.target(name: "<target>", 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.

uienvironment's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.