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:
Swift Packages
tab of your project.https://github.com/sanderrouk/KeyboardLayoutGuide
onto the search bar and click next.Version
option leaving the selection on Up to Next Major
option.import KeyboardLayoutGuide
in the file where you want to use it.github "sanderrouk/KeyboardLayoutGuide" ~> 1.0.0
project to your Cartfile.$ carthage update --platform ios
, this library does not support building for platforms other than iOS.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.
link |
Stars: 1 |
Last commit: 4 years ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics