Giter VIP home page Giter VIP logo

keyboardfriendly's Introduction

KeyboardFriendly

An easy-reuse UIViewController extension to support making screen space for the keyboard, with minimal boilerplate code.

Instructions

To use in any UIViewController, include this extension (KeyboardFriendly.swift) in your project, then in each controller where you want this behavior, do the following:

  1. Make sure the view is wrapped in a top-level UIScrollView, to allow the view to be moved.

  2. Override the computed property:

    var keyboardFriendlyScrollView: UIScrollView?

    to return that scroll view. If this var returns nil (the default), keyboard-friendliness will not be activated.

  3. In viewDidLoad, call self.activateKeyboardFriendliness().

  4. In deinit, call self.deactivateKeyboardFriendliness().

    Alternatively, depending on the needs of the controller, put the activate and deactivate calls in viewDidAppear and viewDidDisappear, respectively. That may be a better choice if the controller is still responding to keyboard events inappropriately while it's offscreen.

  5. Optional โ€” To auto-scroll the view to show the field being edited: Override the computed property:

    var keyboardFriendlyKeepOnscreen: CGRect

    to return the rect area of the current field, or any other rect that you want to keep onscreen. Return CGRect.zero (a.k.a. CGRectZero) to suppress auto-scrolling.1

    Note that the rect must be in the scroll view's coordinate system! Use UIView.convertRect:toView: or UIView.convertRect:fromView: if necessary.

    A simple, basic implementation will track the bounds of the first responder in a concrete instance variable, and have keyboardFriendlyKeepOnscreen return that variable's value:

    var firstResponderBounds: CGRect?
    override var keyboardFriendlyKeepOnscreen: CGRect {
        return self.firstResponderBounds ?? CGRect.zero
    	}

    Use UITextFieldDelegate to keep track of the first responder.

1Why use CGRect.zero to mean "no scrolling" instead of returning an optional? Current Swift limitations (2.2): declarations in an extension can only be overridden in the main class if they're marked @objc, but the CGRect? type can't be represented in Objective-C code.

keyboardfriendly's People

Contributors

mferber avatar

Watchers

 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.