Almost every iOS developer has at one point had to deal with handling the presence of the keyboard. This framework makes it super simple to deal with by creating two new layout guides, keyboardLayoutGuide
and safeAreakeyboardLayoutGuide
. Both are added to the UIView
class through extensions. These can easily constraint to and are updated automatically if a keyboard is presented.
As of Xcode 11 SPM integrates nicely with Xcode. This means that installing dependencies with Xcode support is super easy. To add the dependency using Swift Package Manager do the following steps:
- Select the desired project and choose the
Swift Packages
tab of your project. - Tap on the + button.
- Enter
https://github.com/sanderrouk/KeyboardLayoutGuide
onto the search bar and click next. - Choose the
Version
option leaving the selection onUp to Next Major
option. - Click Next.
- Click Finish.
- Either create a separate file for it or add
import KeyboardLayoutGuide
in the file where you want to use it.
- Add
github "sanderrouk/KeyboardLayoutGuide" ~> 1.0.0
project to your Cartfile. - Run
$ carthage update --platform ios
, this library does not support building for platforms other than iOS. - Do the additional steps required for carthage frameworks.
- Either create a separate file for it or add
import KeyboardLayoutGuide
in the file where you want to use it.
KeyboardLayoutGuide works by simply extending UIKit's UIView
class functionality by adding KeyboardLayoutGuides to it. These can be used the same way the default SafeAreaLayoutGuide can be.
let greenView = UIView()
greenView.translatesAutoresizingMaskIntoConstraints = false
greenView.backgroundColor = .green
view.addSubview(greenView)
NSLayoutConstraint.activate([
greenView.topAnchor.constraint(equalTo: view.safeAreaKeyboardLayoutGuide.topAnchor),
greenView.leftAnchor.constraint(equalTo: view.safeAreaKeyboardLayoutGuide.leftAnchor),
greenView.rightAnchor.constraint(equalTo: view.safeAreaKeyboardLayoutGuide.rightAnchor),
greenView.bottomAnchor.constraint(equalTo: view.safeAreaKeyboardLayoutGuide.bottomAnchor),
])
let redCircle = UIView()
redCircle.translatesAutoresizingMaskIntoConstraints = false
redCircle.backgroundColor = .red
redCircle.layer.cornerRadius = 50
view.addSubview(redCircle)
NSLayoutConstraint.activate([
redCircle.heightAnchor.constraint(equalToConstant: 100),
redCircle.widthAnchor.constraint(equalToConstant: 100),
redCircle.centerXAnchor.constraint(equalTo: view.centerXAnchor),
redCircle.bottomAnchor.constraint(equalTo: view.keyboardLayoutGuide.bottomAnchor)
])
let textField = UITextField()
textField.translatesAutoresizingMaskIntoConstraints = false
textField.placeholder = "Example Text"
view.addSubview(textField)
NSLayoutConstraint.activate([
textField.centerXAnchor.constraint(equalTo: view.centerXAnchor),
textField.centerYAnchor.constraint(equalTo: view.centerYAnchor)
])
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(redCircleTapped))
redCircle.addGestureRecognizer(tapRecognizer)
Or if you use PinKit then you can keep to the same style as this framework brings the familiar syntax.
let greenView = UIView()
greenView.backgroundColor = .green
view.addSubview(greenView)
greenView.pinEdgesToSuperviewSafeAreaKeyboardEdges()
let redCircle = UIView()
redCircle.backgroundColor = .red
redCircle.setDimensions(to: CGSize(width: 100, height: 100))
redCircle.layer.cornerRadius = 50
view.addSubview(redCircle)
redCircle.alignAxisToSuperview(axis: .vertical)
redCircle.pinEdge(toSuperviewKeyboardEdge: .bottom)
let textField = UITextField()
textField.placeholder = "Example Text"
view.addSubview(textField)
textField.centerInSuperview()
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(redCircleTapped))
redCircle.addGestureRecognizer(tapRecognizer)
This framework is planned to be a part of the iOS PowerKit framework which is coming soon.
The project is under the MIT licence meaning you can use this project however you want.