I did some research but still can't find any answers. The scenario is simple:
I have a ViewController, called MainVC
I put 2 observers inside viewDidLoad
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
I have a UITextField, with linked Primary Action Triggered to MainVC, called doneAction
Everytime I press "Return" key, it posts keyboardWillShowNotification, althought there is not a single line of code inside doneAction
Is this some kind of bug or weird reaction of UITextField? Or a offical behavior?
Thanks for any help.
Related
If I open the keyboard when displaying a WKWebView it automatically adds adjustedContentInset to the ScrollView. But the problem is, if I handle the keyboard myself it still adds the adjustedContentInset. How can I fix this?
If you remove the observers which handles the keyboard from the WKWebView it stops adding the adjustedContentInset:
NotificationCenter.default.removeObserver(self.webView, name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
NotificationCenter.default.removeObserver(self.webView, name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.removeObserver(self.webView, name: UIResponder.keyboardWillHideNotification, object: nil)
I currently have a table-view with a textfield acting as a search bar above it. I have the tableview set in interface builder to dismiss the keyboard onDrag. However, whenever I drag down the tableView reloads its data. How can I prevent this?
You could use the keyboard events to enable and disable user interaction on the table, to do this you must register keyboard observers as said in the answer of this question by Jitendra Solanki: iOS Swift detect keyboard events
NotificationCenter.default.addObserver(self, selector: #selector(keyBoardWillShow(notification:)), name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyBoardWillHide(notification:)), name: .UIKeyboardWillHide, object: nil)
And use the callbacks functions to enable and disable the user interaction
func keyBoardWillShow(notification: NSNotification) {
tableView.userInteractionEnabled = NO;
}
func keyBoardWillHide(notification: NSNotification) {
tableView.userInteractionEnabled = YES;
}
But instead ofthe keyboardWillHide, you could use your event of hidekeyboard to reactivate the user interaction.
I have developed custom keyboard. Now I want to give functionality like if some user choose color from my container app and then open my keyboard the background color of my keyboard changes to that color. How to achieve this? Which method gets called every time when some one switch to my custom keyboard so that i can write code on that method?
there is not a predefined method called, but you can set a NSNotification. In your viewdidload method insert:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWasShown:", name: UIKeyboardDidShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillBeHidden:", name: UIKeyboardWillHideNotification, object: nil)
and then add the two methods that will be called the the keyboard appears or disappears:
func keyboardWasShown(notification: NSNotification) {
print("keyboard up")
}
func keyboardWillBeHidden(notification: NSNotification) {
print("keyboard down")
}
But remember to remove the notifications in viewWillDisappear:
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardDidShowNotification, object: self.view.window)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: self.view.window)
bye.
Save the color property as hex string in UserDefaults with suite name. Then use this color in extension app's viewDidLoad method as background color.
I'm trying to get the UITextViewTextDidChangeNotification to work. I'm new to using the NSNotificationCenter, so I'm having a hard time understanding what's going on exactly. I have a UITextView in a storyboard, and I've created an IBOutlet for it in my ViewController class and called it textView.
This is my viewDidLoad function:
override func viewDidLoad() {
super.viewDidLoad()
origin = self.view.frame.origin.y
if let field = textView{
field.placeholder = placeholder
field.layer.cornerRadius = 8
field.layer.borderWidth = 0.5
field.layer.borderColor = UIColor.grayColor().CGColor
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyPressed:"), name:UITextFieldTextDidChangeNotification, object: nil);
}
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil);
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil);
}
The keyboard notifications work great. To my understanding, they call a function with the same name as the selector. Is that correct? Or is there something more going on here? I made a function called keyPressed that took an NSNotification as a parameter, but that function never got called whereas when I engage the keyboard, the keyboardWillShow and keyboardWillHide functions are called. Can someone explain what's going on?
Alright, so I figured out a solution. I needed to post the notification in the textViewDidChange function. I did that using this:
NSNotificationCenter.defaultCenter().postNotificationName("keyPressed", object: nil)
Then I recognized the notification in my viewDidLoad function with this:
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyPressed:"), name:"keyPressed", object: nil);
Then I made this function under my viewDidLoad function:
func keyPressed(sender: NSNotification) {
}
I am aware of the keyboardWillShow and the keyboardWillHide events by:
override public func viewWillAppear(animated: Bool) {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
}
But with the new keyboards in iOS8 the keyboard is able to change without dismissing the keyboard and I was wondering how to call a function on keyboard size change. Anyone know? Thanks.
Edit: It is now calling on frame change but using this code:
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
NSLog("\(keyboardSize.height)")
}
It returns the old keyboard height for example when the frame changes to "224.0" it returns "253.0" as if the height has not updated by time the code is called, and when it goes have to "253.0" it returns the old height again which is "224.0"
Edit 2:
Instead of using "UIKeyboardFrameBeginUserInfoKey", I used "UIKeyboardFrameEndUserInfoKey" and it is now working.
You want UIKeyboardWillChangeFrameNotification and/or UIKeyboardDidChangeFrameNotification.
See the documentation of UIWindow for all of the keyboard related notifications.