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.
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
}
Notification centre for keyboard show/hide not working in iOS 11
Try this updated syntax instead for your observers:
func registerNotificationObservers() {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector:#selector(keyboardWillHide), name: .UIKeyboardWillHide, object: nil)
}
func removeNotificationObservers() {
NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillHide, object: nil)
}
@objc func keyboardWillShow(_ notification: Notification) {
print("keyboardWillShow")
}
@objc func keyboardWillHide(_ notification: Notification) {
print("keyboardWillHide")
}
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)
How do I use #selector?
In Swift 3, it looks like this:
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
Get sender on NotificationCenter keyboard will show
In iOS you can figure out who has the keyboard focus with UIResponder.isFirstResponder (note all UIViews inherit from UIResponder, so its a property on every view). Just check which field has isFirstResponder = true and scroll to that one. Its useful to put all of your fields into an outlet collection if you have a lot of them so you can just iterate through them and figure out which one isFirstResponder.
How to pass data using NotificationCenter in swift 3.0 and NSNotificationCenter in swift 2.0?
Swift 2.0
Pass info using userInfo
which is an optional Dictionary of type [NSObject : AnyObject]?
let imageDataDict:[String: UIImage] = ["image": image]
// post a notification
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "notificationName"), object: nil, userInfo: imageDataDict)
// `default` is now a property, not a method call
// Register to receive notification in your class
NotificationCenter.default.addObserver(self, selector: #selector(self.showSpinningWheel(_:)), name: NSNotification.Name(rawValue: "notificationName"), object: nil)
// handle notification
// For swift 4.0 and above put @objc attribute in front of function Definition
func showSpinningWheel(_ notification: NSNotification) {
if let image = notification.userInfo?["image"] as? UIImage {
// do something with your image
}
}
Swift 3.0, 4.0, 5.0 version and above
The userInfo now takes [AnyHashable: Any]? as an argument, which we provide as a dictionary literal in Swift
let imageDataDict:[String: UIImage] = ["image": image]
// post a notification
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "notificationName"), object: nil, userInfo: imageDataDict)
// `default` is now a property, not a method call
// Register to receive notification in your class
NotificationCenter.default.addObserver(self, selector: #selector(self.showSpinningWheel(_:)), name: NSNotification.Name(rawValue: "notificationName"), object: nil)
// handle notification
// For swift 4.0 and above put @objc attribute in front of function Definition
func showSpinningWheel(_ notification: NSNotification) {
if let image = notification.userInfo?["image"] as? UIImage {
// do something with your image
}
}
NOTE: Notification “names” are no longer strings, but are of type Notification.Name, hence why we are using NSNotification.Name(rawValue: "notificationName")
and we can extend Notification.Name with our own custom notifications.
extension Notification.Name {
static let myNotification = Notification.Name("myNotification")
}
// and post notification like this
NotificationCenter.default.post(name: .myNotification, object: nil)
View Responsiveness on Keyboard Show and Hide Ios Swift 4
The simple solution is to use IQKeyboardManagerSwift.
pod 'IQKeyboardManagerSwift' // add this in your pod file.
Add the following code in didFinishLaunchingWithOptions.
IQKeyboardManager.shared.enable = true
I hope this helps.
Related Topics
Thread 1:Exc_Bad_Access (Code = 1, Address = 0X30000008)
How to Properly Remove Node When Out of Screen Bounds
Can't Send Push Notifications Using the Server API
How to Fix Status Bar Overlap Issue in iOS 7
iOS Custom Annotation: a View Below the Annotation Pin
How to Clear/Reset All Coredata in One-To-Many Relationship
Does iOS Calendar Support a Url Scheme
Objective-C Rabbitmq Client Not Publishing Messages to Queue
Uicollectionviewcell Dynamic Height W/Two Dynamic Labels & Auto Layout
Can the -Objc Flag Be Applied Selectively to Static Libraries
Avaudiosession .Defaulttospeaker Changes Mic Input
How to Implement Tableview Inside Tableview Cell in Swift 3
Nearby Bluetooth Devices Using Swift 3.0
"Invalid Predicate: Nil Rhs" for Second Argument in Nspredicate Format