disable keyboard popup when user taps on uisearchbar - ios

I need some help regarding this issue.
I want to disable the keyboard popup when user taps on a searbar in my app. Any one have an idea on how to do that? It would be even better if it is possible to completely disable keyboard in the app.
I am developing this app in swift for iOS btw.
I have added a searchbar in the view programmatically using the following code:
let searchController: UISearchController!
self.searchController = UISearchController(searchResultsController: nil)
self.searchController.searchResultsUpdater = self
self.searchController.dimsBackgroundDuringPresentation = false
self.searchController.searchBar.sizeToFit()
self.searchController.searchBar.placeholder = "Search"

You just need to add UIKeyboardWillShowNotification in View Did Load
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.keyboardAppears), name: UIKeyboardWillShowNotification, object: nil)
Implement keyboardAppears function in Your Class and resign searchBard Responder.
func keyboardAppears() -> Void {
searchController.searchBar.resignFirstResponder()
}
If you want to disable keyboard for whole app you can follow this link
Close iOS Keyboard by touching anywhere using Swift?

Related

How to show a keyboard appear when click on UILabel in Swift

I trying to show a keyboard when pressing on UILabel using NotificationCenter but the keyboard function is not calling and not showing the keyboard, I really need to show a keyboard appear when pressing on UILabel in Swift.
inside my class
MyClass : UIControl {}
but the keyboard function is not calling
private func _ini(){ let notificationCenter = NotificationCenter.default notificationCenter.addObserver(self, selector: #selector(KeyboardDidShow(_:)), name: UIResponder.keyboardDidShowNotification, object: nil) }
func KeyboardDidShow( myNotification: NSNotification){
print("keyBoardUp")
}
Keyboard only shows up for views which accept inputs e.g. UITextField, UITextview etc or the views which confirm to follow UIKeyInput protocol and implement canBecomeFirstResponder

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)
}

Prevent TableView from reloading data when keyboard is dismissed

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.

Detect events from inside a WKWebView instance

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
}

(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.

Resources