How to write Keyboard notifications in Swift 3
Swift 4
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func keyboardWillShow(notification: NSNotification) {
print("keyboardWillShow")
}
func keyboardWillHide(notification: NSNotification){
print("keyboardWillHide")
}
deinit {
NotificationCenter.default.removeObserver(self)
}
You can also get keyboard info using below code inside these methods.
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange), name: .UIKeyboardWillChangeFrame, object: nil) .
@objc func keyboardWillChange(notification: NSNotification) {
let duration = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double
let curve = notification.userInfo![UIKeyboardAnimationCurveUserInfoKey] as! UInt
let curFrame = (notification.userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
let targetFrame = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
let deltaY = targetFrame.origin.y - curFrame.origin.y
}
How to write Keyboard notifications in Swift 4.2?
Just replace .UIResponder.keyboardDidShowNotification
with UIResponder.keyboardDidShowNotification
and it will solve your problem.
And final code will be:
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardDidShow), name: UIResponder.keyboardDidShowNotification, object: nil)
Swift 3 NSNotificationCenter Keyboardwillshow/hide
Check out the updated Swift Programming Language book. Pages 1027 and 1028 are what you're looking for. It should be something like this:
func keyboardWillHide(_ notification: NSNotification) {…
Notice the additional underscore above. Also:
#selector(LoginViewController.keyboardWillHide(_:))
You also might need to add @objc(keyboardWillHideWithNotification:)
to your class.
Swift 5, How do I get notifications from the keyboard
First of all, I am assuming you are listening to keyboard notification events, i.e. Notification.Name.UIKeyboardWillShow
second, it is not of type NotificationCenter
it is of type Notification
to get the specific Keyboard frame you can use the UIKeyboardFrameEndUserINfoKey
more info on this enum here
example on how to get the frame inside a Notification's userInfo
@objc
func keyboardWillShow(notification: Notification) {
if let keyboardFrame = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue {
let keyboardFrame = keyboardFrame.cgRectValue
//print keyboardFrame.height
}
}
App crashing: Exception - unrecognised selector sent to instance
Three issues:
- The creation of the selector is wrong (use
#selector
) - The signature of the action is wrong (missing underscore)
- The action must be marked as
@objc
And the type in Swift is Notification
without NS
prefix
override func viewDidLoad() {
super.viewDidLoad()
// do stuff...
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
// do other stuff...
}
...
@objc func keyboardWillShow(_ notification: Notification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
self.view.frame.origin.y -= keyboardSize.height
}
}
@objc func keyboardWillHide(_ notification: Notification) {
self.view.frame.origin.y = 0
}
...
How do we make keyboard appear below textView in swift?
One of the easy and no code of line solution is to use the following pods in your app.
IQKeyboardManger
Later you need to just import that in App Delegate and add this two lines of code in didfinishLaunching method:
IQKeyboardManager.sharedManager().enable = true
Your problem will be solved for whole app.
For Swift 5:
IQKeyboardManager.shared.enable = true
Related Topics
Where Is the Official Documentation for Cvopenglestexture Method Types
Lock iOS App in Single App Mode Programmatically
Uitableview Not Visible the Last Cell When Scroll Down
Clipstobounds Causes Uiimage to Not Display in iOS10 & Xcode 8
Xcode/Swift 'Filename Used Twice' Build Error
Best Way to Save and Retrieve Uicolors to Core Data
iOS Input Focused Inside Fixed Parent Stops Position Update of Fixed Elements
Static Linking with Swift, Xcode6-Beta
Get Current View Controller from the App Delegate (Modal Is Possible)
Swrevealviewcontroller Close Rear View When Tapping Front View
How to Install the "App Store" in an iOS Simulator
Autolayout Aspect Ratio for Uiimageview/Uiview
Uiimagejpegrepresentation Has Been Replaced by Instance Method Uiimage.Jpegdata(Compressionquality:)
Set Delegates to Nil Under Arc
Storyboard Instantiateviewcontrollerwithidentifier Not Setting Iboutlets