Never implement NSNotification.Name.UIKeyboardDidShow
ever again. Ever.
Yeah, seriously. Handling the keyboard on iOS shouldn't be painful. But it is.
So instead of doing a whole lot of calculations, or embedding everything in UIScrollView
s, import Keyboard
and get on with your life.
Make your view controller conform to the KeyboardChangeHandler
protocol:
import Keyboard
class ViewController: UIViewController, KeyboardChangeHandler {
Start handling keyboard notifications:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
becomeKeyboardChangeHandler()
}
override func viewWillDisappear(_ animated: Bool) {
resignKeyboardChangeHandler()
super.viewWillDisappear(animated)
}
ctrl + drag to your favorite NSLayoutConstraint
:
Get on with your life
Keyboard
comes with a couple of niceties you might enjoy:
Keyboard
singleton:Keyboard.default.isVisible //tells you if the keyboard is currently visible
Keyboard.default.currentFrame //tells you the keyboard's frame
class ViewController: UIViewController {
override func awakeFromNib() {
super.awakeFromNib()
//do this if you have set up a bunch of outlets on Interface Builder
//but don't want Keyboard to work on this view controller for now
handlesKeyboard = false
}
}
override func viewDidLoad() {
super.viewDidLoad()
[...]
//adds a margin between the keyboard and the currently active text input
//defaults to 40.0
keyboardMargin = 60.0
}
self.view.currentFirstResponder
UIResponder.currentFirstResponder
If the Simulator doesn't show the keyboard straight away, press cmd+K
.
You may get the following messages on the console (which don't affect the library in any way):
_BSMachError: (os/kern) invalid capability (20)
_BSMachError: (os/kern) invalid name (15)
pod 'Keyboard', '~> 1.1'
Then import Keyboard
where needed.
github "BellAppLab/Keyboard" ~> 1.1
Then import Keyboard
where needed.
dependencies: [
.package(url: "https://github.com/BellAppLab/Keyboard", from: "1.1")
]
Then import Keyboard
where needed.
cd toYourProjectsFolder
git submodule add -b submodule --name Keyboard https://github.com/BellAppLab/Keyboard.git
Then drag the Keyboard
folder into your Xcode project.
Bell App Lab, [email protected]
Logo image by Gregor Cresnar from The Noun Project
Keyboard is available under the MIT license. See the LICENSE file for more info.
link |
Stars: 2 |
Last commit: 1 year ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics