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.
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 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
}
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.
Swift 4 show / hide keyboard going over view
You need to replace UIKeyboardFrameBeginUserInfoKey
with UIKeyboardFrameEndUserInfoKey
How to detect when keyboard is shown and hidden
In the ViewDidLoad method of your class set up to listen for messages about the keyboard:
// Listen for keyboard appearances and disappearances
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidHide:)
name:UIKeyboardDidHideNotification
object:nil];
Then in the methods you specify (in this case keyboardDidShow
and keyboardDidHide
) you can do something about it:
- (void)keyboardDidShow: (NSNotification *) notif{
// Do something here
}
- (void)keyboardDidHide: (NSNotification *) notif{
// Do something here
}
Swift: Keyboard Observer via NSNotificationCenter doesn't work
Calling NSNotificationCenter()
is instantiating a new NSNotificationCenter
each time you call it. Try using NSNotificationCenter.defaultCenter()
instead.
Related Topics
When to Use a Colon with a @Selector
How to Handle "Cfnetwork Sslhandshake Failed" in iOS
Why Force Unwrapping Is Required in Case of Enum and Switch
How Get the List of Errors Thrown by a Function
Xmlparser.Sharedparser.Decode() in Swift3
How to Get Email Id of User Using Facebook Sdk 4.7 in iOS 9
How to Tell What Profile/Signing Certificate Was Used to Sign .Ipa
How to Position Views on Top of Each Other
Enable/Disable Apple Push Notification from iPhone App
Highlight Just the Text in a Uilabel
Multiline Uibutton and Autolayout
Set Text Color and Font for Uidatepicker in iOS8/Swift
Swift - Checking Unmanaged Address Book Single Value Property for Nil
How to Get a Crash Log Due to Expiration of Provisioning Profile