UISearchBar Show keyboard - swift

I have an action to call searchBar. When it is called, i want the keyboard to show up.
How can i show the keyboard?
This is the code with my action:
#IBAction func Searappear(sender: AnyObject) {
searchBar.hidden=false
navigationController?.navigationBarHidden = true
}

You should call searchBar.becomeFirstResponder().

Related

UITapGestureRecognizer to hide keyboard taps on UITableViewCell

I have the below script, and when the keyboard is up, if I click around it in this UIController, it will hide the keyboard, but it will also click on the cell. I do not want that to happen.
I know that cancelsTouchesInView will control that feature... and if I set it to true, it wont click on the cell, BUT I can't click on the cell after the keyboard is hidden.
Is there a good solution for this?
extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tap = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard2))
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}
#objc func dismissKeyboard2() {
view.endEditing(true)
}
}
Your best bet may be to disable user interaction on your tableView when the keyboard is shown, and then re-enable it when you end editing.
However, you might give this a try and see if it does what you need.
Change your signature for the dismissKeyboard2 func to get a reference to the tap gesture recognizer and remove it when you end editing:
extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard2(_:)))
tapGesture.cancelsTouchesInView = true
view.addGestureRecognizer(tapGesture)
}
#objc func dismissKeyboard2(_ g: UITapGestureRecognizer) {
view.endEditing(true)
view.removeGestureRecognizer(g)
}
}

How to hide an action button in Swift?

I try to make a button invisible after pressing it. The Connection is an action and not an outlet, because pressing the button will call additional code.
#IBAction func startGame(_ sender: Any) {
print("The game starts...")
}
This does not work, because the button is an action and not an outlet:
startGame.isHidden = true
Is there another way to make an action button invisible and therefore not clickable?
Just create an IBOutlet of the same button and set its isHidden property to true once it's tapped.
#IBAction func startGame(_ sender: Any) {
startGameButton.isHidden = true
}
You can hide button on pressed action this way
#IBAction func startGame(_ sender: Any) {
let pressedButton : UIButton = sender as! UIButton
pressedButton.isHidden = true;
}
You can rewrite your code a little bit as Pratik suggested, so it will look like this:
#IBAction func startGame(_ sender: UIButton) {
sender.isHidden = true
/*
remove button at all from the parent view.
sender.removeFromSuperview()
*/
print("The game starts...")
}

How can I prevent UISearchController dismiss when cancel button pressed?

I want to just hide keyboard when user first press cancel button.
Just likes AppStore.app
I use UISearchController like that:
navigationItem.searchController = searchController
============== Update ==============
This Cancel button is hosted by UISearchController.
If you did search bar programmatically, you can add extension on your ViewController or else just use function call as below.
extension ViewController: UISearchBarDelegate {
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
if let text = searchBar.text {
self.filterContent(text: text)
searchBar.resignFirstResponder()
}
}
}

swift becomeFirstResponder

I need to open the keyboard.
When the ViewController starts I use this function
func showKeyboardAndFocus(){
DispatchQueue.main.async {
self.placeForText.resignFirstResponder()
self.placeForText.becomeFirstResponder()
}
}
from viewDidLoad and it works correctly.
When I close it, and try to open using the same function, it doesn't work.
Why? How can I open it second time?
To show the keyboard:
self.inputTextView.becomeFirstResponder()
To hide the keyboard:
self.inputTextView.resignFirstResponder()
Show/hide second time
#IBAction func showKeyboardClicked(_ sender: UIButton) {
textField.becomeFirstResponder()
}
#IBAction func closeKeyboardClicked(_ sender: Any) {
textField.resignFirstResponder()
}

How to make the cancel button of a searchBar accessible ?

I have a searchbar, that opens a keyboard when selected.
The issue is, when I use VoiceOver, the keyboard stays open even when moving on to another element. For that, I wish to use the cancel button of the searchbar and make it appear only if accessibility is used, but when I make it appear, VoiceOver does not read it.
How can I make it accessible ? Or do you know any other way to handle properly the keyboard of a searchbar with VoiceOver ?
public func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
if UIAccessibilityIsVoiceOverRunning() {
self.searchBar?.setShowsCancelButton(true, animated: true)
}
}
public func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
self.searchBar?.showsCancelButton = false
}