Prevent TableView from reloading data when keyboard is dismissed - ios

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.

Related

ios/swift - How to disable keyboard?

I have a simple iOS app with two controllers. One of them holds a webview displaying a website. The website has its own keyboard which appears when editing html input fields. However, when trying to use one of these inputs iOS displays its own built in keyboard.
How to disable keyboard in iOS/swift from popping up in a single controller or within a web view?
You can try
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
///
#objc func keyboardWillShow(notification: NSNotification) {
self.view.endEditing(true)
}

How to hide (cover) all button when an action is not complete

I have keyboard is showing on screen and a Reveal menu Button.
When I tap menu Button, the reveal tableView is shown but keyboard is not hidden.
I want to cover menu Button (can not tap button) if keyboard is showing, it is like UIAlertViewController or like UIActivityViewController, you can not make another action before the alert or activity is completed.
I will add an hide keyboard button to keyboard, but user have to tap this button before tap the reveal menu button.
No need to complicate this issue, you could simply create a notifier for your keyboard to handle the interaction for your keyboard.
1: Add these rows in your viewDidLoad function
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
2: Add these functions to enable/disable user interaction for your button
func keyboardWillShow(_ notification: NSNotification) {
button.isUserInteractionEnabled = false
}
func keyboardWillHide(_ notification: NSNotification) {
button.isUserInteractionEnabled = true
}
To hide the keyboard self.view.endEditing(true).

Which method is called when my custom keyboard is shown on screen?

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.

(Swift) Moving views out of the way for keyboard?

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.

Keyboard size changed event in swift?

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.

Resources