How to make keyboard dismiss when I press out of searchbar on Swift?
try this :
self.mySearchController.searchBar.endEditing(true)
replace mySearchController with your created controller name..
If you did not create it programmatically but instead you just dragged a search bar from library then IBoutlet your searchable to your class and reference it as:
self.mySearchBar.endEditing(true)
Make keyboard disappear when clicking outside of Search Bar - SWIFT
You can use this extension.
extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}
@objc func dismissKeyboard() {
view.endEditing(true)
}
}
Usage. In your viewController:
override func viewDidLoad() {
super.viewDidLoad()
hideKeyboardWhenTappedAround()
}
How to dismiss keyboard when pressing Search key on tableView in swift 3?
If you are using UISearchBar then first make sure its delegate is set
and then add this function
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder()
}
How to dismiss keyboard when the UISearch Bar 'Search button is pressed?
From Text, Web and Editing Programming Guide for iOS:
To dismiss the keyboard, you call the resignFirstResponder method of the text-based view that is currently the first responder.
So you should do this in your UISearchBarDelegate
:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
// Do the search...
}
iOS - How to dismiss keyboard from the navigationItem.searchController when tap anywhere on the UIView?
There is no need to add UITapGestureRecognizer
as proposed above. UIViewContoller
already conforms to UIResponder
interface (legacy from Objective C), so you can override this method like this:
extension UIViewController {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.window?.endEditing(true)
super.touchesEnded(touches, with: event)
}
}
SwiftUI Wrapped SearchBar Can't Dismiss Keyboard
The reason searchBarCancelButtonClicked
is not being called is because it is in MySearchBar
but you have set the Coordinator
as the search bars delegate. If you move the searchBarCancelButtonClicked
func to the Coordinator
, it will be called.
Here is what the coordinator should look like:
class Coordinator: NSObject, UISearchBarDelegate {
@Binding var sText: String
init(sText: Binding<String>) {
_sText = sText
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
sText = searchText
}
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
searchBar.text = ""
searchBar.resignFirstResponder()
searchBar.showsCancelButton = false
searchBar.endEditing(true)
}
}
How to dismiss UISearchBar keyboard without effecting other functionalities?
Set cancelsTouchesInView
to false, for detailed explanation: link
extension UIViewController {
func hideKeyboard() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}
@objc
func dismissKeyboard() {
view.endEditing(true)
}
}
Usage:
call hideKeyboard()
in the viewDidLoad of the controller.
Dismiss keyboard when pressing x in UISearchBar
Just resign the first responder but in the next run loop like this:
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if ([searchText length] == 0)
{
[searchBar performSelector:@selector(resignFirstResponder)
withObject:nil
afterDelay:0];
}
}
Tested and it works.
Related Topics
Grab Frames from Video Using Swift
Swift 4.2 Imagepickercontroller Issue
Viewcontroller Slide Animation
How to Reference Swift Playground Itself
When and How to Use @Noreturn Attribute in Swift
Swift 2 Protocol Extension Not Calling Overridden Method Correctly
Draw a Hole in a Rectangle with Spritekit
How Constant Is the Firebase Anonymous Id
Add Animations to Foreach Loop Elements (Swiftui)
How to Tell If a Node Is on the Screen Spritekit Swift
Swift Update Label (With HTML Content) Takes 1Min
How Do People Deal with Iterating a Swift Struct Value-Type Property
Checking If an Array of Custom Objects Contain a Specific Custom Object
What Does the '@' Symbol Mean in Swift
How to Calculate the 21! (21 Factorial) in Swift
How to Simulate Mouse Click from MAC App to Other Application