Convert Figma logo to code with AI

michaeltyson logoTPKeyboardAvoiding

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

5,811
925
5,811
102

Top Related Projects

⛔️**DEPRECATED** ⛔️ A drop-in UIViewController subclass with a growing text input view and other useful messaging features

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.

DAKeyboardControl adds keyboard awareness and scrolling dismissal (ala iMessages app) to any view with only 1 line of code.

Block-based animations made easy, comes with easing functions and a CASpringAnimation replacement.

Quick Overview

TPKeyboardAvoiding is an iOS library that provides a drop-in universal solution for preventing keyboards from covering text fields in your app. It automatically adjusts the scroll view insets or content offset to ensure that the active text field remains visible when the keyboard appears.

Pros

  • Easy to implement with minimal code changes
  • Works with UIScrollView, UITableView, and UICollectionView
  • Handles both portrait and landscape orientations
  • Supports iOS 5.0 and later

Cons

  • May not be suitable for complex custom layouts
  • Limited customization options for animation behavior
  • Not actively maintained (last update was in 2019)
  • Might conflict with other scroll view management libraries

Code Examples

  1. Basic usage with UIScrollView:
class ViewController: UIViewController {
    @IBOutlet weak var scrollView: TPKeyboardAvoidingScrollView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // No additional setup required
    }
}
  1. Using with UITableView:
class TableViewController: UITableViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView = TPKeyboardAvoidingTableView(frame: self.tableView.frame, style: self.tableView.style)
    }
}
  1. Manually adjusting content insets:
let scrollView = TPKeyboardAvoidingScrollView(frame: view.bounds)
scrollView.contentInset = UIEdgeInsets(top: 20, left: 0, bottom: 20, right: 0)
scrollView.scrollIndicatorInsets = scrollView.contentInset

Getting Started

  1. Add the TPKeyboardAvoiding folder to your Xcode project.
  2. Import the library in your view controller:
    import TPKeyboardAvoiding
    
  3. Replace your UIScrollView, UITableView, or UICollectionView with the corresponding TPKeyboardAvoiding subclass:
    let scrollView = TPKeyboardAvoidingScrollView(frame: view.bounds)
    view.addSubview(scrollView)
    
  4. Add your text fields or text views to the scroll view as usual.

That's it! The library will automatically handle keyboard avoidance for you.

Competitor Comparisons

⛔️**DEPRECATED** ⛔️ A drop-in UIViewController subclass with a growing text input view and other useful messaging features

Pros of SlackTextViewController

  • More feature-rich, offering a complete messaging UI solution
  • Includes auto-growing text view and customizable input accessory view
  • Provides built-in support for typing indicators and message sending

Cons of SlackTextViewController

  • Heavier and more complex, potentially overkill for simpler projects
  • Less focused on just keyboard avoidance, which may not be needed in all cases
  • Requires more setup and configuration compared to TPKeyboardAvoiding

Code Comparison

TPKeyboardAvoiding:

- (void)keyboardWillShow:(NSNotification*)notification {
    [self adjustOffsetToIdealIfNeeded];
}

SlackTextViewController:

- (void)textViewDidChange:(UITextView *)textView {
    [self updateTextViewHeight:textView];
    [self scrollToBottomAnimated:YES];
}

TPKeyboardAvoiding focuses solely on adjusting content offset when the keyboard appears, while SlackTextViewController handles text view changes, updates its height, and scrolls the content as needed.

SlackTextViewController offers a more comprehensive solution for messaging interfaces, including features like auto-growing text views and typing indicators. However, it may be overly complex for projects that only need simple keyboard avoidance. TPKeyboardAvoiding, on the other hand, provides a lightweight and focused solution for keyboard avoidance but lacks additional messaging-specific features.

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.

Pros of IQKeyboardManager

  • More comprehensive solution with additional features like keyboard appearance customization and text field management
  • Automatic handling of keyboard events without manual setup
  • Active development and frequent updates

Cons of IQKeyboardManager

  • Larger codebase and potentially higher memory footprint
  • May introduce unnecessary complexity for simpler projects
  • Some users report occasional conflicts with custom UI implementations

Code Comparison

TPKeyboardAvoiding:

override func viewDidLoad() {
    super.viewDidLoad()
    scrollView.keyboardDismissMode = .interactive
    TPKeyboardAvoiding.setup(scrollView)
}

IQKeyboardManager:

override func viewDidLoad() {
    super.viewDidLoad()
    IQKeyboardManager.shared.enable = true
    IQKeyboardManager.shared.enableAutoToolbar = false
    IQKeyboardManager.shared.shouldResignOnTouchOutside = true
}

Both libraries aim to solve the issue of keyboard obstruction in iOS apps, but IQKeyboardManager offers a more feature-rich solution at the cost of increased complexity. TPKeyboardAvoiding provides a simpler, lightweight approach that may be sufficient for many projects. The choice between the two depends on the specific requirements of your app and the level of customization needed for keyboard management.

DAKeyboardControl adds keyboard awareness and scrolling dismissal (ala iMessages app) to any view with only 1 line of code.

Pros of DAKeyboardControl

  • More customizable with options for animation duration and keyboard height
  • Supports both UIScrollView and UIView
  • Includes a demo project for easier implementation understanding

Cons of DAKeyboardControl

  • Less actively maintained (last update in 2017)
  • Requires more setup code compared to TPKeyboardAvoiding
  • May have compatibility issues with newer iOS versions

Code Comparison

TPKeyboardAvoiding:

#import <TPKeyboardAvoiding/TPKeyboardAvoidingScrollView.h>

TPKeyboardAvoidingScrollView *scrollView = [[TPKeyboardAvoidingScrollView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:scrollView];

DAKeyboardControl:

#import "DAKeyboardControl.h"

[self.view addKeyboardPanningWithActionHandler:^(CGRect keyboardFrameInView) {
    // Handle keyboard frame changes
}];

TPKeyboardAvoiding offers a simpler implementation with a custom scroll view subclass, while DAKeyboardControl provides more flexibility but requires additional setup code. TPKeyboardAvoiding is more actively maintained and likely to be compatible with recent iOS versions, whereas DAKeyboardControl offers more customization options but may require updates for newer iOS releases.

Block-based animations made easy, comes with easing functions and a CASpringAnimation replacement.

Pros of RBBAnimation

  • Focuses on complex animations and transitions
  • Provides a more flexible and customizable animation system
  • Supports keyframe-based animations

Cons of RBBAnimation

  • Steeper learning curve due to its more advanced features
  • May be overkill for simple UI adjustments like keyboard avoidance
  • Less specific to keyboard-related issues

Code Comparison

TPKeyboardAvoiding:

- (void)keyboardWillShow:(NSNotification*)notification {
    TPKeyboardAvoidingState *state = self.keyboardAvoidingState;
    UIView *firstResponder = [self TPKeyboardAvoiding_findFirstResponderBeneathView:self];
    state.keyboardRect = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    state.keyboardVisible = YES;
    state.priorInset = self.contentInset;
    state.priorScrollIndicatorInsets = self.scrollIndicatorInsets;
    
    // ...
}

RBBAnimation:

RBBTweenAnimation *animation = [RBBTweenAnimation animationWithKeyPath:@"position.y"];

animation.fromValue = @(view.center.y);
animation.toValue = @(view.center.y - 100);
animation.duration = 0.3;
animation.timingFunction = RBBEasingFunctionEaseInEaseOut;

[view.layer addAnimation:animation forKey:@"slide"];

While TPKeyboardAvoiding is specifically designed for handling keyboard-related UI adjustments, RBBAnimation provides a more general-purpose animation framework for creating complex animations and transitions in iOS applications.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

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 proposed solutions out there for how to move UITextField and UITextView out of the way of the keyboard during editing -- usually, it comes down to observing UIKeyboardWillShowNotification and UIKeyboardWillHideNotification, or implementing UITextFieldDelegate delegate methods, and adjusting the frame of the superview, or using UITableView's scrollToRowAtIndexPath:atScrollPosition:animated:, but most proposed solutions tend to be quite DIY, and have to be implemented for each view controller that needs it.

This is a relatively universal, drop-in solution: UIScrollView and UITableView subclasses that handle everything.

When the keyboard is about to appear, the subclass will find the subview that's about to be edited, and adjust its frame and content offset to make sure that view is visible, with an animation to match the keyboard pop-up. When the keyboard disappears, it restores its prior size.

It should work with basically any setup, either a UITableView-based interface, or one consisting of views placed manually.

It also automatically hooks up "Next" buttons on the keyboard to switch through the text fields.

Usage

For use with UITableViewController classes, drop TPKeyboardAvoidingTableView.m and TPKeyboardAvoidingTableView.h into your project, and make your UITableView a TPKeyboardAvoidingTableView in the xib. If you're not using a xib with your controller, I know of no easy way to make its UITableView a custom class: The path of least resistance is to create a xib for it.

For non-UITableViewControllers, drop the TPKeyboardAvoidingScrollView.m and TPKeyboardAvoidingScrollView.h source files into your project, pop a UIScrollView into your view controller's xib, set the scroll view's class to TPKeyboardAvoidingScrollView, and put all your controls within that scroll view. You can also create it programmatically, without using a xib - just use the TPKeyboardAvoidingScrollView as your top-level view.

To disable the automatic "Next" button functionality, change the UITextField's return key type to anything but UIReturnKeyDefault.

Notes

These classes currently adjust the contentInset parameter to avoid content moving beneath the keyboard. This is done, as opposed to adjusting the frame, in order to work around an iOS bug that results in a jerky animation where the view jumps upwards, before settling down. In order to facilitate this workaround, the contentSize is maintained to be at least same size as the view's frame.

Licence (Zlib)

Copyright (c) 2013 Michael Tyson

This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.

Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.

  2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.

  3. This notice may not be removed or altered from any source distribution.


Michael Tyson, A Tasty Pixel
michael@atastypixel.com