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.
Related
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 know how to detect a normal keyboard being shown in Swift but I'm wondering if it's possible to detect the events inside a WKWebView because... if the app gets in the background, inputs lose focus but the "blur" event isn't triggered.
The idea is that I have a "navigation bar" in the app which gets pushed out of the view (upwards) when the keyboard is being shown, and I'd like to keep showing it. Knowing the keyboard is about 216px tall I'd just like to narrow the height of the content wrapper which is flex based by 216px but that's not really working since it is an animation which I can't really reproduce so it's smooth. I also can't detect when user went into emoji tab. I got rid of the autocorrect since I just set it to off for the input tag.
Try this code.
override viewWillAppear(){
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil);
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide"), name:UIKeyboardWillHideNotification, object: nil);
}
func keyboardWillHide(){
navigationController?.setNavigationBarHidden(false, animated: true)
}
func keyboardWillShow(notification: NSNotification) {
navigationController?.setNavigationBarHidden(true, animated: true)
var info = notification.userInfo!
var keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
// handle your layout according to frame
}
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 am having a weird problem with moving my views to make room for an incoming keyboard. Basically, in my app, there is a button that performs a segue which pushes a new instance of a view controller that is embedded inside of a navigation controller modally. Within this first instance, my keyboard code works perfectly. The code is as follows:
func keyboardWillShow(sender: NSNotification) {
if !keyBoard {
self.view.frame.origin.y -= 200
}
keyBoard = true
}
func keyboardWillHide(sender: NSNotification) {
if keyBoard {
self.view.frame.origin.y += 200
}
keyBoard = false
}
with the following in viewDidLoad:
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)
I also added a tapGestureRecognizer so that when the keyboard is showing and the user taps anywhere on the screen, the keyboard closes:
let tapped = UITapGestureRecognizer(target: self, action: "closeKeyboard")
tapped.numberOfTapsRequired = 1
self.view.addGestureRecognizer(tapped)
and
func closeKeyboard() {
self.view.endEditing(true)
}
So in the first instance, this code works perfectly. However, after I go back, calling self.dismissViewControllerAnimated, and, once I am on the original screen, click the button that calls performSegue again and push a new instance of this same view controller, the code breaks and the view no longer moves out of the way of the keyboard but just sort of stutters and bounces a little. I have no idea why this is happening and any help would be very much appreciated. Thanks!
Try putting a breakpoint on keyboardWillShow and keyboardWillHide, and tell me if both the methods are called when you go back to the second ViewController.
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) {
}