Giter VIP home page Giter VIP logo

ios.blog.keyboard_avoidance's Introduction

Keyboard Avoidance

⌨️ Keyboard avoidance with a single line of code. Animate to text fields (or any responder), swipe down keyboard dismissal for free. See the corresponding article for more Keyboard Avoidance with a single line of code, also on detail how to backport keyboardLayoutGuide to iOS 13 and iOS 14.

UIKit_Keyboard_Avoidance_Sign_Up_Form_KeyboardLayoutGuide.mp4

How

  1. Put everything in a scroll view.
  2. Pin the bottom of the scroll view to the top of the keyboardLayoutGuide.
  3. Set keyboardDismissMode to .onDrag.
class ViewController: UIViewController {
    
    lazy var emailTextField = UITextField()
        .withEmailStyle
        .with(next: givenNameTextField)
    
    lazy var givenNameTextField = UITextField()
        .withGivenNameStyle
        .with(next: familyNameTextField)
    
    lazy var familyNameTextField = UITextField()
        .withFamilyNameStyle
        .with(next: passwordTextField)
    
    lazy var passwordTextField = UITextField()
        .withPasswordStyle
    
    lazy var signUpButton = UIButton()
        .withSignUpButtonStyle
    
    lazy var content = UIStackView()
        .vertical(spacing: UI.spacing)
        .views(
            HeaderView()
                .withFixedHeight,
            emailTextField,
            givenNameTextField,
            familyNameTextField,
            passwordTextField,
            signUpButton
        )
    
    lazy var body = UIScrollView()
        .with {
            $0.addSubview(content)

            // 💎
            $0.keyboardDismissMode = .onDrag
            $0.bounces = false
        }
        .onMoveToSuperview { scrollView in
            self.content.pin(to: scrollView)
            self.content.widthAnchor.constraint(equalTo: scrollView.widthAnchor).isActive = true
        }
    
    var bottomConstraint: NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .systemBackground
        
        // Hierarchy.
        view.addSubview(body)
        body.translatesAutoresizingMaskIntoConstraints = false
        body.topAnchor.constraint(equalTo: view.topAnchor, constant: UI.padding).isActive = true

        // 💎
        body.bottomAnchor.constraint(
            equalTo: view.keyboardLayoutGuide.topAnchor,
            constant: -UI.spacing
        ).isActive = true

        body.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor, constant: UI.spacing).isActive = true
        body.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor, constant: -UI.spacing).isActive = true
    }
}

The example above uses Withable, a simple inline decorator that helps simplify your UIKit code.

License

Licensed under the MIT License.

ios.blog.keyboard_avoidance's People

Contributors

geri-borbas avatar

Stargazers

 avatar  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.