Swift Dismiss UITextView Keyboard - swift

I am having trouble dismissing the keyboard of a text view in swift.
I was able to dismiss a textfield with the following
#IBAction func DismissKeyboard(sender: AnyObject)
{
self.resignFirstResponder()
}
But I'm not sure how I go about it with a text view

You have to set the textview.delegate to self and use the shouldChangeTextInRange function to resign on pressing return.
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
if text == "\n" // Recognizes enter key in keyboard
{
textView.resignFirstResponder()
return false
}
return true
}

swift 3
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if text == "\n" {
textView.resignFirstResponder()
return false
}
return true
}

just add the UITextView as an IBOutlet and use the same function:
#IBAction func DismissKeyboard(sender: AnyObject) {
yourtextView.resignFirstResponder()
}

This is a much better more-intuitive (for your user) solution for dismissing soft keyboard. The problem with the "\n" solution is the user cannot insert line-breaks in the textView-- as hitting the return/done button on the keyboard will dismiss the keyboard before the line break occurs. The method below lets you keep that functionality while teaching the user that just tapping outside text-entry fields is a standard way of dismissing keyboards.
// MARK: - dismiss keyboard when user taps outside of text boxes
override func viewDidLoad() {
super.viewDidLoad()
configureTapGesture()
}
private func configureTapGesture() {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(WriteJournalEntryViewController.handleTap))
view.addGestureRecognizer(tapGesture)
}
#objc func handleTap() {
view.endEditing(true)
}
(assumes this code is inside a UIViewController custom subclass named "WriteJournalEntryViewController).
view = your entire view in the UIViewController, so it works for more than just your textView object.
endEditing = a method for UIView class, you can read its documentation.

Related

UITextView Delegate textViewDidChange is not called

So I really am sorry if my post is a little off, this is my first time asking question.
I have a subclass of UITextView, let's call it A.
class A: UITextView{
var customDelegate: CustomTextViewDelegate!
}
extension A: UITextViewDelegate{
func textViewDidChange(_ textView: UITextView){
if let delegate = customDelegate{
delegate.textViewDidChange(textView)
}
}
func textViewShouldChangeTextIn(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if let delegate = customDelegate{
return delegate.textViewShouldChangeTextIn(baseTextView: textView, range: range, text: text)
}
return true
}
}
protocol CustomTextViewDelegate{
func textViewDidChange(_ customTextView: UITextView)
}
class SecondVC: UIViewController{}
extension SecondVC: CustomTextViewDelegate{
func textViewShouldChangeTextIn(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool
return false //this is where the problem is
}
As you can see, A delegate UITextViewDelegate to itself. I instantiate A to two different ViewController. in FirstVC, textViewDidChange method from the delegate is getting called normally, but on the secondVC it does not.
Now, here is the problem, any other method from UITextViewDelegate is getting called normally on SecondVC like textView(ShouldChangeTextIn), textViewDidEndEditing. Do anyone know what is going on? and where the problem could come from?
I have tried to make sure that not a single line of code try to assign the textViewDelegate to other class.
I have found the issue, I want to simplify things when I made this post by not mentioning quite few things. But, let's just say that I make CustomTextViewDelegate so that whatever VC instantiate the A (CustomTextView) can still implement their own custom "UITextViewDelegate". The problem lies when SecondVC TextView(shouldChangeText in) from CustomTextViewDelegate return false, therefore TextViewDidChange is not getting called.

numeric keyboard resigns to normal key board in swift

I have a normal set up for a textfield with numberpad and the dismiss keyboard and resignFirstResponder actions in place, but when I tap on the background the numeric keyboard changes to the normal keyboard and then when I tap the background again the keyboard dismisses - STRANGE! How can I dismiss the numeric keyboard with one tap to the background?
self.textField.delegate = self
textField.keyboardType = UIKeyboardType.DecimalPad
#IBAction func tapBackground(sender: AnyObject) {
view.endEditing(true)
}
#IBAction func viewTapped(sender: AnyObject) {
textField.resignFirstResponder()
}
Try following this may help you :)
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: "handleTapGesture:")
tap.delegate = self
self.view.addGestureRecognizer(tap)
}
// MARK: - UITapGestureRecognizer
func handleTapGesture(gesture : UITapGestureRecognizer) {
self.view.endEditing(true)
}
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool
{
return true
}

UITextView keyboard is taking 2 taps to open.

I have a UITextView with a label over it as a placeholder. When the user taps on the UITextView the label disappears but for the keyboard to appear it takes another 2 taps. When I remove the tap gesture that hides the label the keyboard works perfectly. Here is my code any ideas as to what the problem is???
var tapTerm:UITapGestureRecognizer = UITapGestureRecognizer()
override func viewDidLoad() {
super.viewDidLoad()
bioTextfield.delegate = self
tapTerm = UITapGestureRecognizer(target: self, action: "tapTextView:")
// bioPlaceholderLabel.addGestureRecognizer(tapTerm)
bioTextfield.addGestureRecognizer(tapTerm)
}
func tapTextView(sender:UITapGestureRecognizer) {
// hide placeholder label text
bioPlaceholderLabel.text = ""
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// when user touches outside the keyboard close the keyboard
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}
// when user presses the return button close the keyboard
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
if text == "\n" {
textView.resignFirstResponder()
return false
}
return true
}
Any help would be greatly appreciated!
Easier way, you should use this custom UITextView (with place holder): KMPlaceholderTextView. I'm using, so great.

How to make keyboard dismiss when I press out of searchbar on Swift?

I try to make my searchbar on swift, but I have a problem to dismiss keyboard on screen when I pressed out of searchbar. When I try with textfield, it works perfectly fine with this code.
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
self.view.endEditing(true)
}
It work when i press out of my textfield and then the keyboard is gone. I want to make like that with my searchbar, because when I use searchbar and use the same way like textfield, it doesn't work at all. Any reference or code is very useful for me.
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)
I found it easier and simplier to use Table View for dismissal. (If you're using table view)
Swift 4:
self.tableView.keyboardDismissMode = .onDrag
Tested and working!
func searchBarSearchButtonClicked(searchBar: UISearchBar)
{
searchActive = false;
self.mySearchBar.endEditing(true)
}
Edit for Swift 4.2
func searchBarSearchButtonClicked(_ searchBar: UISearchBar)
{
searchActive = false
self.searchBar.endEditing(true)
}
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
searchActive = false;
searchProject.resignFirstResponder()
}
This method will be invoked when user click search button on keyboard.So here we can dismiss keyboard.I think this is the right method.
Firstly, Apple's UISearchBarDelegate is the correct solution to hide keyboard when users click a search button while UISearchBar's instance is the first responder (learn UIResponder). In short, searchBarSearchButtonClicked(_:) is what you need for this task.
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder() // hides the keyboard.
doThingsForSearching()
}
If it doesn't work, check, does your controller conform to UISearchBarDelegate and secondly, does UISearchBarDelegate know about your class implementation (if you don't quite understand what am I talking about, you should learn delegation pattern starting to read here):
class YourAwesomeViewController: UIViewController, UISearchBarDelegate { // pay attention here
#IBOutlet weak var yourSearchBar: UISearchBar!
override func viewDidLoad() {
super.viewDidLoad()
self.yourSearchBar.delegate = self // and it's important too
}
}
Further, if you need to hide the keyboard touching outside of search bar without touching the search button (the user may change his mind to search something), UITapGestureRecognizer is a simple way too to deal with that.
Ctrl-drag a Tap Gesture Recognizer from the Object Library to your View Controller.
Ctrl-drag the recently added Tap Gesture Recognizer from the document outline in the storyboard to your class implementation as IBAction.
Finally, write a code:
#IBAction func tapToHideKeyboard(_ sender: UITapGestureRecognizer) {
self.yourSearchBar.resignFirstResponder()
}
Also, don't forget to create #IBOutlet for the search bar to have an access inside your class implementation.
Both variants above work well in my project.
Swift 4+:
You can try, creating a tap gesture and add in the self.view
let singleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.singleTap(sender:)))
singleTapGestureRecognizer.numberOfTapsRequired = 1
singleTapGestureRecognizer.isEnabled = true
singleTapGestureRecognizer.cancelsTouchesInView = false
self.view.addGestureRecognizer(singleTapGestureRecognizer)
and in selector func you call self.searchBar.resignFirstResponder
#objc func singleTap(sender: UITapGestureRecognizer) {
self.searchBar.resignFirstResponder()
}
You can use a general UIViewController extension
Just add a new swift file on the project and paste the following code snippet
Code
extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard(_:)))
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}
#objc func dismissKeyboard(_ sender: UITapGestureRecognizer) {
view.endEditing(true)
if let nav = self.navigationController {
nav.view.endEditing(true)
}
}
}
Now call hideKeyboardWhenTappedAround() from viewDidLoad method where you want keyboard hiding feature.
class MaCaveViewController: UIViewController, UISearchBarDelegate {
#IBOutlet weak var searchBar: UISearchBar!
override func viewDidLoad() {
super.viewDidLoad()
searchBar.delegate = self
}
// When button "Search" pressed
func searchBarSearchButtonClicked(_ searchBar: UISearchBar){
print("end searching --> Close Keyboard")
self.searchBar.endEditing(true)
}
}
This works very well for me.
we can do this with following methods
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
searchBar.showsCancelButton = true;
}
func searchBarTextDidEndEditing(searchBar: UISearchBar) {
searchBar.showsCancelButton = false;
}
This works for me in Swift 4
func searchBarSearchButtonClicked(_ searchBar: UISearchBar){
self.searchBar.endEditing(true)
}

How to dismiss keyboard for UITextView

The following triggers when the Done button is clicked on the keyboard. It goes into the conditional and returns false. However, the keyboard remains on the screen.
class MyViewController: UIViewController, UITextViewDelegate{
func textView(textView: UITextView!, shouldChangeTextInRange: NSRange, replacementText: NSString!) -> Bool {
if(replacementText.isEqualToString("\n")) {
textBox.resignFirstResponder()
return false
}
return true
}
textBox is a delegate for the view. I have connected this through Interface Builder. Any ideas why it isn't dismissing the keyboard?
First of all please ensure that your My ViewController is a delegate of the textField,
And then, on the method shouldChangeTextInRange use the textView that is passed in the delegate method instead of your property textBox.
For example:
class MyViewController: UIViewController, UITextViewDelegate{
func viewDidLoad(){
super.viewDidLoad()
textBox.delegate = self
}
func textView(textView: UITextView!, shouldChangeTextInRange: NSRange, replacementText: NSString!) -> Bool {
if(replacementText.isEqualToString("\n")) {
textView.resignFirstResponder()
return false
}
return true
}