Coder Social home page Coder Social logo

rmkitty / keyboardobserving Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nickffox/keyboardobserving

0.0 0.0 0.0 348 KB

⌨️A Combine-based way to observe and adjust for Keyboard notifications in SwiftUI

License: MIT License

Swift 93.48% Ruby 6.52%

keyboardobserving's Introduction

⌨️ Keyboard Observing

A Combine-based solution for observing and avoiding the keyboard in SwiftUI.

Swift Support Platform CocoaPods Compatible SwiftPM Compatible

Table of Contents

About

This package give you the ability to observe changes to keyboard state using the Keyboard ObservableObject type.

It also provides a KeyboardObservingView that adjusts its content to avoid the keyboard, and a .keyboardObserving() ViewModifier that adjusts the modified view to avoid the keyboard.

Demo

Requirements

  • iOS 13.0+
  • Xcode 11+
  • Swift 5.1+

Installation

This package can be installed using CocoaPods or Swift Package Manager.

CocoaPods

Add the following line to your Podfile:

pod 'KeyboardObserving'

For more information about how to get started with CocoaPods, check out the CocoaPods website.

Swift Package Manager

Add the following to your Package.swift file:

dependencies: [
    .package(
        url: "https://github.com/nickffox/KeyboardObserving.git", 
        .branch:("master")
    )
]

If you're using SPM through Xcode:

  1. Go to File > Swift Packages > Add Package Dependency
  2. Enter https://github.com/nickffox/KeyboardObserving
  3. Select the branch option, and type "master"

For more information about how to get started with the Swift Package Manager, check out the Official SPM website or the SPM project on GitHub.

Usage

Using the KeyboardObserving ViewModifier

Add the .keyboardObserving() ViewModifier to your custom SwiftUI view.

import KeyboardObserving

struct YourView: View {

  var body: some View {
    VStack {
      // Your Content Here
    }
    .keyboardObserving()
  }
}

Using Keyboard and KeyboardObservingView

1. Add a Keyboard to your environment

In your SceneDelegate.swift file, add a Keyboard property, and add it to your scene's environment.

import KeyboardObserving

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

  var window: UIWindow?

  // A Keyboard that will be added to the environment.
  var keyboard = Keyboard()


  func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).

    // Use a UIHostingController as window root view controller
    if let windowScene = scene as? UIWindowScene {
      let window = UIWindow(windowScene: windowScene)
      window.rootViewController = UIHostingController(
        rootView: YourRootView()
          // Adds the keyboard to the environment
          .environmentObject(keyboard)
      )
      self.window = window
      window.makeKeyAndVisible()
    }
  }
}
2. Create your View

Add your view's content inside of a KeyboardObservingView .

import KeyboardObserving

struct YourView: View {

  var body: some View {
    KeyboardObservingView {
      // Your content goes here!
    }
  }
}

keyboardobserving's People

Contributors

nickffox avatar daisuke-t-jp avatar jaywardell avatar juliankahnert avatar ldiqual avatar macbellingrath avatar mkj-is avatar maxdesiatov avatar ryu0118 avatar

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.