Showing / dismissing keyboard animation in simple UIViewController category.

Overview

RSKKeyboardAnimationObserver License Carthage compatible

Easy way to handle iOS keyboard showing/dismissing.

Introduction

Working with iOS keyboard demands a lot of duplicated code. This category allows you to declare your animations with smooth keyboard animation timing while writing very little code.

Demo

KeyboardAnimationDemo1 KeyboardAnimationDemo2

Installation

RSKKeyboardAnimationObserver requires iOS 6.0 or later.

Using CocoaPods

  1. Add the pod RSKKeyboardAnimationObserver to your Podfile.

    pod 'RSKKeyboardAnimationObserver'
    
  2. Run pod install from Terminal, then open your app's .xcworkspace file to launch Xcode.

  3. Import the RSKKeyboardAnimationObserver.h header. Typically, this should be written as #import <RSKKeyboardAnimationObserver/RSKKeyboardAnimationObserver.h>

Using Carthage

  1. Add the ruslanskorb/RSKKeyboardAnimationObserver project to your Cartfile.

    github "ruslanskorb/RSKKeyboardAnimationObserver"
    
  2. Run carthage update, then follow the additional steps required to add the iOS and/or Mac frameworks into your project.

  3. Import the RSKKeyboardAnimationObserver framework/module.

    • Using Modules: @import RSKKeyboardAnimationObserver
    • Without Modules: #import <RSKKeyboardAnimationObserver/RSKKeyboardAnimationObserver.h>

Example

Imagine that you need to implement chat-like input over keyboard. OK, import this category.

#import <RSKKeyboardAnimationObserver/RSKKeyboardAnimationObserver.h>

Then make autolayout constraint between your input bottom and superview botton in Interface Builder, connect it with your view controller implementation through IBOutlet.

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *chatInputBottomSpace;

Then subscribe to keyboard in the place you like (viewDidAppear is the best place really).

__weak typeof(self) weakSelf = self;
[self rsk_subscribeKeyboardWithWillShowOrHideAnimation:^(CGRect keyboardRectEnd, NSTimeInterval duration, BOOL isShowing) {
    __strong typeof(self) strongSelf = weakSelf;
    if (strongSelf) {
        strongSelf.chatInputBottomSpace.constant = isShowing ?  CGRectGetHeight(keyboardRectEnd) : 0;
        [strongSelf.view layoutIfNeeded];
    }
} onComplete:nil];

That’s all!

Don’t forget to unsubscribe from keyboard events (viewDidDisappear is my recommendation). Calling this category method will do all the “dirty” work for you.

[self rsk_unsubscribeKeyboard];

For more complex behaviour (like in demo section) you can use extended API call with before animation section.

__weak typeof(self) weakSelf = self;
[self rsk_subscribeKeyboardWithBeforeWillShowOrHideAnimation:^(CGRect keyboardRectEnd, NSTimeInterval duration, BOOL isShowing) {
    __strong typeof(self) strongSelf = weakSelf;
    if (strongSelf) {
        strongSelf.isKeaboardAnimation = YES;
        
        [UIView transitionWithView:strongSelf.imageView duration:duration options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
            if (isShowing) {
                strongSelf.imageView.image = [strongSelf.imageView.image applyLightEffect];
            } else {
                [strongSelf.imageView hnk_setImageFromURL:strongSelf.model.cardImageUrl];
            }
        } completion:nil];
    }
 } willShowOrHideAnimation:^(CGRect keyboardRectEnd, NSTimeInterval duration, BOOL isShowing) {
    __strong typeof(self) strongSelf = weakSelf;
    if (strongSelf) {
        strongSelf.headerHeight.constant = isShowing ? kHeaderMinHeight : kHeaderMaxHeight;
        strongSelf.panelSpace.constant = isShowing ?  CGRectGetHeight(keyboardRectEnd) : 0;
        
        for (UIView *v in strongSelf.headerAlphaViews) {
            v.alpha = isShowing ? 0.0f : 1.0f;
        }

        [strongSelf.view layoutIfNeeded];
    }
} onComplete:^(BOOL finished, BOOL isShown) {
    __strong typeof(self) strongSelf = weakSelf;
    if (strongSelf) {
        strongSelf.isKeaboardAnimation = NO;
    }
}];

Contact

Ruslan Skorb

License

This project is is available under the MIT license. See the LICENSE file for more info. Attribution by linking to the project page is appreciated.

You might also like...
Best way to dismiss Keyboard in a View Controller iOS (Swift)
Best way to dismiss Keyboard in a View Controller iOS (Swift)

Best way to dismiss Keyboard in a View Controller iOS (Swift) First way: Implement UITextFieldDelegate’s textFieldShouldReturn method and dismiss curr

Objective-C library for tracking keyboard in iOS apps.
Objective-C library for tracking keyboard in iOS apps.

NgKeyboardTracker Objective-c library for tracking keyboard in iOS apps. Adding to your project If you are using CocoaPods, add to your Podfile: pod '

For less complicated keyboard event handling.

KeyboardObserver For less complicated keyboard event handling. Features Less complicated keyboard event handling. Do not use Notification , but event

A drop-in universal solution for moving text fields out of the way of the keyboard in iOS

TPKeyboardAvoiding A drop-in universal solution for moving text fields out of the way of the keyboard in iOS. Introduction There are a hundred and one

Emoji Keyboard SDK (iOS)
Emoji Keyboard SDK (iOS)

Makemoji SDK Makemoji is a free emoji keyboard for mobile apps. By installing our keyboard SDK every user of your app will instantly have access to ne

A Chinese keyboard for iOS that helps Chinese language learners remember tones.
A Chinese keyboard for iOS that helps Chinese language learners remember tones.

ToneBoard ToneBoard is a Chinese keyboard for iOS that requires you to enter the correct tones while typing simplified Chinese with Pinyin. It is avai

SwiftyKeyboard: a full customized numeric keyboard for iOS
SwiftyKeyboard: a full customized numeric keyboard for iOS

SwiftyKeyboard Overview SwiftyKeyboard is an iOS customized enhanced keyboard. T

Interactive Keyboard Controller for Swift
Interactive Keyboard Controller for Swift

Keynode Why Using UIScrollViewKeyboardDismissMode added in iOS7, interactive keyboard operation became possible. But, it only works on UIScrollView. K

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

⌨️ Keyboard Observing A Combine-based solution for observing and avoiding the keyboard in SwiftUI. Table of Contents About Requirements Installation C

Owner
Ruslan Skorb
"The best way to predict the future is to create it" – Abraham Lincoln. Let's create our future together! Founder & Principal Software Engineer @ R.SK Lab
Ruslan Skorb
KeyboardMan helps you to make keyboard animation.

KeyboardMan We may need keyboard infomation from keyboard notifications to do animation. However, the approach is complicated and easy to make mistake

null 353 Apr 19, 2022
A simple keyboard to use with numbers and, optionally, a decimal point.

MMNumberKeyboard A simple keyboard to use with numbers and, optionally, a decimal point. Installation From CocoaPods CocoaPods is a dependency manager

Matías Martínez 957 Nov 17, 2022
IHKeyboardAvoiding is an elegant solution for keeping any UIView visible when the keyboard is being shown - no UIScrollView required!

IHKeyboardAvoiding An elegant solution for keeping any UIView visible when the keyboard is being shown Requirements IHKeyboardAvoiding Version Objecti

Idle Hands Apps 1.4k Dec 14, 2022
Codeless drop-in universal library allows to prevent issues of keyboard sliding up and cover UITextField/UITextView. Neither need to write any code nor any setup required and much more.

IQKeyboardManager While developing iOS apps, we often run into issues where the iPhone keyboard slides up and covers the UITextField/UITextView. IQKey

Mohd Iftekhar Qurashi 15.9k Jan 8, 2023
Codeless manager to hide keyboard by tapping on views for iOS written in Swift

KeyboardHideManager KeyboardHideManager - codeless manager to hide keyboard by tapping on views for iOS written in Swift. Structure Features Requireme

Bondar Yaroslav 55 Oct 19, 2022
⌨️ Add user-customizable global keyboard shortcuts to your macOS app in minutes

This package lets you add support for user-customizable global keyboard shortcuts to your macOS app in minutes. It's fully sandbox and Mac App Store c

Sindre Sorhus 1.1k Dec 29, 2022
Swift UIKit keyboard manager for iOS apps.

Typist Typist is a small, drop-in Swift UIKit keyboard manager for iOS apps. It helps you manage keyboard's screen presence and behavior without notif

Toto Tvalavadze 1.1k Dec 10, 2022
Suppress mouse & keyboard events on MacOSX. Baby-proof my Mac!

Suppress mouse & keyboard events on MacOSX Catches all events (mouse, keyboard, everything), and either consumes them (locked state) or passes them th

Albert Zeyer 6 Oct 21, 2022
KeyboardKit is a Swift library that helps you create custom keyboard extensions for iOS and ipadOS.

KeyboardKit is a Swift library that helps you create custom keyboard extensions for iOS and ipadOS.

KeyboardKit 900 Jan 9, 2023
QMK Agent is a macOS menubar application which sends commands to a QMK enabled keyboard

QMKagent QMK Agent is a macOS menubar application which sends commands to a QMK enabled keyboard Features System volume indicator using top row (Esc t

Mike Killewald 4 Apr 24, 2022