iOS - Dismiss keyboard on Return key [duplicate] - ios

This question already has answers here:
dismiss keyboard with a uiTextView
(6 answers)
Closed 7 years ago.
I am working with a UITextView and I need to dismiss the keyboard by pressing return key.
This is what I'm doing:
class MyController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var writeNote: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
self.writeNote.delegate = self
}
how to proceed? When using a UITextField I would simply call the delegate for textFieldShouldReturn method but I don't appear to find anything like that for a textView. I can not use a textField because I need a larger amount of rows. The TextView tooltip says "When a user taps a text view, a keyboard appears; when a user taps Return in the keyboard, the keyboard disappears and..." but it doesn't do it by default..

UITextView doesn't have any such direct delegate method to hide the keyboard. You have to work around with the shouldChangeTextInRange: replacementTextmethod.
In this method, you can check for a new line character and if you get one, you can resign.

Related

Custom keyboard on UITextView shows apple keyboard first then switches to the custom keyboard

Short video of what happens when tapping on a textView:
https://www.youtube.com/watch?v=LG_r3iDJKiM
I'm trying to show my custom keyboard on a UITextView from the textViewDidBeginEditing function, The custom keyboard has been tested on UITextFields and is working as intended. Now I'm trying to get the same keyboard to work with UITextViews.
When opening the app for the first time, and tapping on a textView the first time will have this behavior, then when we tap on other textViews the custom keyboard is there directly as intended.
the Current code in my TextViewDidBeginEditing, I tried changing the order of the last 4 lines but with no success:
public func textViewDidBeginEditing(_ textView: UITextView) {
keyboard = Keyboard.sharedInstance.keyboardWithType(availableKeyboards.numbers.rawValue, returnKey:textView.returnKeyType, isSecure: false,decimalSeparator:self.decimalSeparatorCharacter, responderClass: self)
inputView.keyboardAppearance = .dark
inputView.hideAssistantBar()
inputView.inputView = keyboard
inputView.reloadInputViews()
}
This will result in the video above, I tried some other ways but no success yet:
Setting a empty UIView as the inputView, this will still result in the apple keyboard shortly appearing
let view:UIView = UIView()
textView.inputView = view
also tried doing reloadInputViews but that doesn't work either:
textView.reloadInputViews()
also tried running it all on the main tread with
DispatchQueue.main.async { }
but no success, I cannot understand why even setting a UIView() as the inputView will still shortly show the apple keyboard. Any help in the right direction would be greatly appreciated!
Thanks in advance!

Get Keyboard frame without keyboardWillAppear userinfo notification, iOS

I am developing an iPhone app in which I have to show some customize picker in case of a button event. But I don't want to hard coded values for the frame of my custom pickerView. I searched and found keyboardWillShow notification userInfo method, but in my case I am not showing keyboard so can't get frame out of it. Can anybody help me way out for getting keyboard frame that I can use for my customize picker?
You can use a UITextField and call becomeFirstResponder and immediately resignFirstResponder on it, so it will be shown and hidden without actually seeing the keyboard.
the action method of the button will look like the following:
#IBAction func showPickerView(sender: AnyObject)
{
let textField = UITextField()
view.addSubview(textField)
textField.becomeFirstResponder()
textField.resignFirstResponder()
textField.removeFromSuperview()
}
And you could listen to the notification and get the height.
for further information: Get height of iOS keyboard without UIKeyboardWillShowNotification

Swift 2.1 - Inputing information into a TextView which populates a Textfield at the same time?

I am currently using a framework called "Swift Validator", it only allows for textfields to be used. In my case I need a textView, my question is how do I input information that I type into the textview which then populates a textfield in the background at the same time? as well as when removing inputed text?
Use the UITextViewDelegate method textViewDidChange to listen for changes to the Text View. When the method is called, update the Text Field. Here's an example:
func textViewDidChange(theTextView: UITextView) {
self.textField.text = theTextView.text
}
Don't forget to extend UITextViewDelegate and to set the Text View's delegate to self.

While displaying an alert in IOS, keyboard does not resign from View

While displaying an alert(Wrong password) in IOS 8, keyboard opens automatically and hide the alert(just in Iphone 4s because of the screen's size), so I can't click in "OK" and I also can't dismiss keyboard because first I need to close the alert!
Keyboard hides alert
(It seems the app is recovering last keyboard's state and showing up again)
How can I close the keyboard before calling the alert?(this way the state will be "closed")
I've tried:
myTextField!.resignFirstResponder()
While calling the button, but it didn't work, the alert shows up and automatically the keyboard opens over it !
if myTextField!.resignFirstResponder() is not working properly try this when you present the alert before call this -->self.view.endEditing(true)
the above function is not work well , try
Choice -1 :Using the Responder Chain
UIApplication.sharedApplication().sendAction("resignFirstResponder", to:nil, from:nil, forEvent:nil)
This will resign the first responder (and dismiss the keyboard) every time, without you needing to send resignFirstResponder to the proper view. No matter what, this will dismiss the keyboard. It’s by far the best way to do it: no worrying about who the first responder is
Choice -2 :UIView’s endEditing
(assuming your text field is a subview of the view you call this on). Most of the time:
self.view.endEditing(true)
set Delegate to myTextField
Include
func textFieldShouldReturn(textField: UITextField) -> Bool
{
textField .resignFirstResponder()
return true
}
Other wise Try the following
var activeField : UITextField!
func textFieldDidBeginEditing(textField: UITextField)
{
activeField = textField
}
func textFieldDidEndEditing(textField: UITextField)
{
activeField = nil
}
func textFieldShouldReturn(textField: UITextField) -> Bool
{
textField .resignFirstResponder()
return true
}
Call activeField.resignFirstResponder() before alert appears
I think from iOS8 you need to use UIAlertController instead of UIAlertView. Using UiAlertView in iOS8 and above is causing keyboard to popup unnecessarily. I have seen this and i made a condition to use UIAlertController for iOS8 and above. In below version UIAlertView should work fine
This is UIAlertView bug on iOS 8.
I have same problem but UIAlertController has not problem. :3
UIAlertView was deprecated since iOS8.
https://developer.apple.com/reference/uikit/uialertview
In Swift 4 : below code worked for me
DispatchQueue.main.async() {
self.view.endEditing(true)
}

Remove focus from the textfield

I am doing two operations on a textfield:
1) To move the textfield using pan gesture.
2) To write on the textfield when the user clicks on it.
Concern is that I want to perform one operation at a time i.e when the user is writing he cannot scroll and vice versa.
Name of UIButton action --- button
Name of UIButton outlet --- optionButton
- (IBAction)button:(id)sender
{
if([_optionButton.titleLabel.text isEqualToString:#"SCROLL"])
{
NSLog(#"can scroll");
NSLog(#"POINT=============%d ",point);
point=0;
[_optionButton setTitle:#"WRITE" forState:UIControlStateNormal];
textField1.enabled=NO;
}
else
{ NSLog(#"can write");
NSLog(#"POINT=============%d ",point);
point=1;
[_optionButton setTitle:#"SCROLL" forState:UIControlStateNormal];
textField1.enabled=YES;
}
}
The point variable is controlling the complete operation and is set to 1 at the beginning.This means that the user can write in the beginning and if he want to write he has to press the button.
The problem that I am facing is that by using "textField1.enabled=NO" I am not able to scroll the textfield.
Is there any function using which I can remove the focus from the textfield but can scroll it.
You can use this code to remove focus (As said in one of the comment),
[textField1 resignFirstResponder];
you can also remove focus from text field by calling [textField1 endEditing:YES]
An UITextfield only has 1 line, so you can´t scroll it.
UPDATE
If you want to move your UITextfield with your finger use UIPanGestureRecognizer in your parent view and apply the changes in your uitextfield.
You can simply follow the steps:
Implement UITextFieldDelegate into your Controller
class ClassName: UIViewController, UITextFieldDelegate {....}
Implement the "textFieldShouldBeginEditing" function
class ClassName: UIViewController, UITextFieldDelegate {
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
return false
}
}
So returning false in textFieldShouldBeginEditing will simply not allow to edit into the UITextField.

Resources