• Stars
    star
    458
  • Rank 95,591 (Top 2 %)
  • Language
    Swift
  • License
    MIT License
  • Created over 4 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A modern, real iOS keyboard system notifications handler framework that Just Works.

KeyboardGuide

A modern, real iOS keyboard system notifications handler framework that Just Works.

KeyboardGuide

As you know, handling the keyboard on iOS was just a nightmare.

On the internet, there are many, many iOS keyboard handler implementations for system notifications such as UIResponder.keyboardWillChangeFrameNotification (UIKeyboardWillChangeFrameNotification), but most of them are not implemented properly.

For example, many of them are not converting the keyboard frame in the right coordinate or not considering iPad keyboard behaviors. Also, there are many undocumented behaviors that are not consistent between each iOS version.

This framework is solving this problem.

Based on years experience of iOS application development and various tests on each iOS version and device, it supports both Swift and Objective-C and works mostly reasonably on the latest 3 versions of iOS, which is iOS 11, 12 and iOS 13 now, and covers almost all iOS users.

Usage

Using KeyboardGuide is really simple. See also Examples for actual usage, that contains Swift and Objective-C code for testing.

Add KeyboardGuide framework to your project

Add the following lines to your Package.swift or use Xcode “Add Package Dependency…” menu.

// In your `Package.swift`

dependencies: [
    // ...
    .package(url: "https://github.com/niw/KeyboardGuide.git"),
    // ...
]

Using KeyboardGuide with the other dependency management tools.

KeyboardGuide doesn’t support directly the other dependency management tools than Swift Package Manager and CocoaPods. It is highly recommend to use Swift Package Manager, if possible. However, since it has no dependencies other than system frameworks, you can still use it from the other dependency management tools.

CocoaPods

To use KeyboardGuide from CocoaPods, add following line in your Podfile.

pod 'KeyboardGuide'

Carthage

To use KeyboardGuide from Carthage, update Cartfile in your project.

github "niw/KeyboardGuide"

Then, run following commands. This will create Carthage/Build/iOS/KeyboardGuide.framework.

$ carthage update
$ (cd Carthage/Checkouts/KeyboardGuide && swift package generate-xcodeproj)
$ carthage build --platform iOS

Follow the instruction to add the framework and Run Script phase to your project.

Setup KeyboardGuide

Add import KeyboardGuide.

Use KeyboardGuide.shared.activate() to activate KeyboardGuide at the beginning of application life cycle, such as application(_:didFinishLaunchingWithOptions:).

// In your `UIApplicationDelegate`

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    KeyboardGuide.shared.activate()
    // ...
}

Add constraints on your views

Lay out your views by adding constraints to view.keyboardSafeArea.layoutGuide, where represents the safe area from, not covered by the keyboard.

It works as like safeAreaLayoutGuide for the notch.

// In your `UIViewController`

override func viewDidLoad() {
    super.viewDidLoad()
    // ...
    let textView = UITextView()
    textView.translatesAutoresizingMaskIntoConstraints = false
    // ...
    textView.bottomAnchor.constraint(equalTo: view.keyboardSafeArea.layoutGuide.bottomAnchor).isActive = true
    // ...
}

Manual layout

If you prefer to manually lay out your views or need to use length of keyboard safe area, for such as updating UIScrollView’s contentInset, override viewDidLayoutSubviews() or layoutSubviews() as like regular manual lay outing.

// In your `UIViewController`

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    // ...
    let bottomInset = view.keyboardSafeArea.insets.bottom
    textView.contentInset.bottom = bottomInset
    textView.scrollIndicatorInsets.bottom = bottomInset
}

The important layout behavior

If your view doesn’t have any sub views that have at least one constraint to view.keyboardSafeArea.layoutGuide, UIKit MAY NOT call layoutSubviews (or similar callbacks such as viewDidLayoutSubviews).

Because in general, the keyboard safe area has a relative layout relationship between the keyboard position where is outside of the view but in the screen, UIKit doesn’t know that relationship without a constraint.

For example, on iOS 13, if a view controller is presented as a modal on iPad in portrait, when the keyboard is appearing, that modal view controller is moved upwards by UIKit. This move changes the relative position of your view to the keyboard, however, UIKit doesn’t call your layoutSubviews (which is obvious,) but for keyboard safe area, you MAY need to re lay out your sub views.

Therefore, you MAY need to add at least one constraint to view.keyboardSafeArea.layoutGuide from one of your sub views, to let UIKit knows view has that relationship.

Known limitations

There are a few known limitations in the current implementation. All limitations are currently To-Do of this project.

  • No SwiftUI support yet.

  • Share extension can’t use this library yet because of UIApplication dependency.

More Repositories

1

HapticKey

A simple utility application to trigger haptic feedback when tapping Touch Bar.
Objective-C
1,649
star
2

iphone_opencv_test

Test application for iPhone with OpenCV library
C++
373
star
3

AlpacaChat

A Swift library that runs Alpaca-LoRA prediction locally to implement ChatGPT like app on Apple platform devices.
C
348
star
4

ramen

🍜 A repository to organize self-made ramen recipes.
137
star
5

GoodMacAppIcon

What is a good Mac app icon?
Swift
60
star
6

poxen

Automation tool to configure your Mac.
Ruby
54
star
7

Echo

A simple iOS application redirects microphone input to the Bluetooth audio device
Swift
44
star
8

TinyLinux

A really tiny minimum implementation of Virtualization framework to boot Linux.
Swift
38
star
9

HolePunch

🕳 A simple command line tool to punch hole to reduce disk usage on APFS volume for such as a raw disk image.
Swift
36
star
10

WindowAlignment

A simple application to align windows on macOS.
Swift
36
star
11

GraphQLLanguage

A simple GraphQL language parser library for Swift.
Swift
26
star
12

mozc

A mirror repository of Mozc, an open source Japanese input method made by Google.
C++
24
star
13

profiles

dot files and more
Vim Script
22
star
14

node-base128

Encode, decode binary to/from UTF-8 string using Base128.
JavaScript
21
star
15

rubies

A script to switch Ruby.
Shell
20
star
16

growl_reeder

A tiny SIMBL plugin to enable Growl desktop notification in awesome Reeder.app for Mac
C
15
star
17

automatic_linking_tool

A simple command line tool to check Mach-O binary configuration for automatic linking
Objective-C
13
star
18

clang_plugins

A repository for clang plugins development
CMake
12
star
19

open_with_default

A tiny extension for Google Chrome on Mac OS X to send links to Safari or Firefox etc.
C
11
star
20

tweetie_hack

Enhance awesome Tweetie for Mac
Objective-C
10
star
21

terminal_coloreopard

Change Terminal.app ANSI Color on Leopard
Objective-C
9
star
22

Hidppgui

A companion helper app for using Logitech mouse on macOS.
Swift
9
star
23

zoommy

A Stylish Image Zooming JavaScript
JavaScript
9
star
24

safari_hack

Improve Safari.app behavior a little bit, like display favicon in tab bar.
Objective-C
8
star
25

skype_logger

A Simple Plain Text Logger Plugin for Skype.app
Ruby
8
star
26

scalding_examples

A very simple Scalding examples, test codes and a helper script.
Puppet
7
star
27

userscripts

JavaScript
6
star
28

readability_importer

A script imports many URLs into Readability.
Ruby
6
star
29

sparkle_appcast

A simple Sparkle `appcast.xml` tool
Ruby
6
star
30

gmail_contrast_theme

Contrast Theme for Gmail Chrome Extension
CSS
6
star
31

chemr

CHM viewer for MacOS X, Forked from original SVN repository
Ruby
6
star
32

DisableKey

A super simple tool to disable key inputs from internal keyboard.
Objective-C
5
star
33

storm_examples

A very simple Storm examples, example codes and a helper configuration.
Scala
5
star
34

antlr4-graphql-grammar

A simple ANTLR4 grammar that can parse GraphQL language (June 2018 Edition)
ANTLR
4
star
35

hidkeys

A tiny USB firmware for Atmel AVR.
C
4
star
36

iOSDCJapan2020Example

An example for the presentation at iOSDC Japan 2020
Swift
4
star
37

iphone_emorywhere

A demo application to enable Emoji keyboard everywhere on the iPhone
Objective-C
4
star
38

SimpleVideoPlayer

A very simple video player for iOS
Objective-C
4
star
39

DisplayAlignment

A simple command line tool to align displays on macOS.
Swift
4
star
40

growl_transition_duration_patch

A sample of Method Swizzling and SIMBL plugin to make Growl notification faster
Objective-C
4
star
41

prowl_tweets

Prowl client for Twitter
Ruby
3
star
42

OpenApplication

A simple command line interface that uses `LSOpenApplication` to open application with preserving environment variables and arguments.
Objective-C
3
star
43

limechat_themes

Themes for LimeChat
3
star
44

intellij-enable-color-management-plugin

A tiny plugin to hot-fix a color management issue on IntelliJ running on the embedded JDK
Java
3
star
45

meowatch

Apple Watch app which you can Meow anytime, anywhere ― on your wrist.
Swift
3
star
46

jquery_plugins

Plugins for jQuery
JavaScript
3
star
47

HIDPP

A Swift module to communicate with Logitech, Inc. keyboards or mouses by using their proprietary HID++ protocol.
Swift
3
star
48

electron-messenger

Yet another simple shell application for Messenger using Electron.
JavaScript
2
star
49

cfddns

A simple command line tool to update Cloudflare DNS record.
Rust
2
star
50

standalone_mod_dav

Standalone WebDav server using mod_dav
Shell
2
star
51

type_pad_template

A command and gem to edit TypePad advanced templates.
Ruby
2
star
52

oauth_client

A tiny command, library to invoke OAuth requests.
Ruby
2
star
53

webapp_apis

A chunk of scripts for some web applications
Ruby
2
star
54

AnimationPlayer

No interesting things here.
Swift
2
star
55

Translator

A simple macOS app that translates Japanese to English, or English to Japanese.
Swift
2
star
56

sf_covid19

A commad line tool and library for COVID-19 in San Francisco.
Ruby
1
star
57

javas

A simple Java version switcher for OS X.
Shell
1
star
58

homebrew-additions

Additional Formulas to Homebrew.
Ruby
1
star
59

wemo_device

A simple Ruby gem library that lookups Belkin’s Wemo devices.
Ruby
1
star
60

test

To understand how GitHub works, this repository is a test playground.
1
star
61

capistrano-forwarding

Capistrano extension providing SSH port forwarding while deploying.
Ruby
1
star
62

self_sizing_auto_layout_cell_example

This is an example application that describes how using self-sizing auto-layout cell is difficult.
Objective-C
1
star
63

vendorized_bundler

A small script to make Bundler and dependencies vendorized.
Ruby
1
star
64

hiera-osxkeychain

Hiera backend for looking up OS X keychain
Ruby
1
star
65

InlineVideoPlayer

A small sample project to inline video player within texts on `UITextView`.
Swift
1
star
66

mfk5

A prototype keyboard that contains enough features to make a full size split keyboard.
Shell
1
star
67

nodemcu_air_quality_sensor

A simple sensor node for measuring air quality in your room.
C++
1
star
68

ports

private portfiles which includes some patch, configuration
C
1
star
69

iterm2-git-svn-clone

iTerm2, cloned from original SVN repository
Objective-C
1
star
70

swift-contribution-workspace

A workspace repository for contributing to Swift and related projects on macOS
Shell
1
star
71

FixEvernoteSnippetsView

A dynamic link library that patches Evernote snippets view that has wrong configuration
Objective-C
1
star
72

buildroot-docker

A simple build environment for Buildroot by using Docker.
Dockerfile
1
star
73

Geso

Swift
1
star
74

mfk60

60% ANSI Layout Split Keyboard. mfk60 is My First Keyboard, 60%.
Python
1
star