Simple solution for keyboard avoiding. View that manages it's bottom constraint constant or frame height to avoid keyboard.
To run the example project, clone the repo, and run pod install
from the Example directory first.
If you are setting KeyboardAvoidingView
class in storyboard assure module field is also KeyboardAvoidingView
Please check official guide
Cartfile:
github "APUtils/KeyboardAvoidingView" ~> 5.2
Then add both KeyboardAvoidingView
and ViewState
frameworks to your project. Remove APExtensionsViewState
dependency if you previously had it.
KeyboardAvoidingView is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "KeyboardAvoidingView", '~> 5.2'
The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler.
Once you have your Swift package set up, adding KeyboardAvoidingView
as a dependency is as easy as adding it to the dependencies value of your Package.swift
.
dependencies: [
.package(url: "https://github.com/APUtils/KeyboardAvoidingView.git", .upToNextMajor(from: "5.2.0"))
]
Just set KeyboardAvoidingView
class to any view in storyboard (usually it's base container) that you want to adjust it's bottom constraint or frame height to avoid keyboard, assure module field is also KeyboardAvoidingView
.
It's also possible to create it from code:
let keyboardAvoidingView = KeyboardAvoidingView(frame: containerView.bounds)
keyboardAvoidingView.translatesAutoresizingMaskIntoConstraints = true
keyboardAvoidingView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
containerView.addSubview(keyboardAvoidingView)
In most cases it should be only one KeyboardAvoidingView
for screen. Be sure to not create nested KeyboardAvoidingView
's.
See example project for more details.
KeyboardAvoidingView
more likely will conflict with IQKeyboardManager
so you have to disable IQKeyboardManager
on the screen you are using KeyboardAvoidingView
. Please reffer to the IQKeyboardManager
documentation. More likely it'll look something like this:
IQKeyboardManager.shared.disabledDistanceHandlingClasses.append(contentsOf: [MyViewController.self])
Any contribution is more than welcome! You can contribute through pull requests and issues on GitHub.
Anton Plebanovich, anton.plebanovich@gmail.com
KeyboardAvoidingView is available under the MIT license. See the LICENSE file for more info.