How to hide the keyboard programmatically in iphone - ios

How to hide the keyboard programmatically in iphone?

Tell the UIResponder subclass that is currently first responder to resign its first responder status:
[responder resignFirstResponder];

[textFieldName resignFirstResponder];

It's easy:
ObjC
[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
Swift
UIApplication.shared.keyWindow?.endEditing(true)
take a look at UIView Class Reference for endEditing.
Causes the view (or one of its embedded text fields) to resign the first responder status. And keyWindow is the only window that receives keyboard events, so this solution is guarantied to always work.

Call this in your ViewController
[self.view endEditing:YES];

If you are using textview then
- (BOOL)textView:(UITextView *)textView
shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text
{
if ([text isEqualToString:#"\n"])
{
[textView resignFirstResponder];
[self keyboardWillHide];
}
}
and if you are using textfield then
-(BOOL)textFieldShouldReturn:(UITextField*)textField;
{
[textField resignFirstResponder];
}

Here is the swift version:
UIApplication.sharedApplication().sendAction("resignFirstResponder", to:nil, from:nil, forEvent:nil)

Related

iOS Keyboard doesn't resign until after view transition

I have set this UIViewController to be the delegate for the UITextField in the viewDidLoad with this line: self.nameInputTextField.delegate = self;.
I have set the delegate on the class as well by adding <UITextFieldDelegate> to the #interface declaration.
When I select the nextButton, in the method that is called, I have tried [self.nameInputTextField resignFirstResponder] as well as [self.view endEditing:YES] one line before I push the new view controller.
The rest of the class does not manipulate the firstResponder.
I've also implemented the UITextField delegate method
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[self.nameInputTextField resignFirstResponder];
return NO;
}
I haven't found any related questions after extensive searching. There are many similar ones about resigning keyboards, but not regarding the timing of the keyboard resignation being postponed until after the view transition is complete. Note- if you reload this url in your browser, you'll see the gif again from the beginning.
Hide keyboard anywhere in ios :
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UIView * txt in self.view.subviews){
if ([txt isKindOfClass:[UITextField class]] && [txt isFirstResponder]) {
[txt resignFirstResponder];
}
}
}
OR
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}
Resign your keyboard on viewWillDisappear and the problem should be solved.
Edit
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
self.nameInputTextField.text = #"";
[self.nameInputTextField resignFirstResponder];
}

re-focus UItextfield after rotation of device

I am reloading my table with reloaddata on rotation of device in my app. If a UITextfield is focused before rotation and keyboard is open , I want it to stay focused and keyboard remains open. Reloaddata calls "textfieldshouldEndEditing" and "KeyboardshouldHide" notifications and thus on rotation my textfield is not focused and keyboard is also closed.
To achieve this I am using the following code:
[myTextfield performSelector:#selector(becomeFirstResponder)
withObject:nil
afterDelay:1.0f];
This works fine and keyboard opens again after rotating the device but the problem is when I press keyboard hide button (now keyboard is closed) and then rotate the device, Keyboard is still showing up which is wrong as before rotating the device I closed it.
Can someone suggest what is wrong or what should be done to achieve this? Thanks
How about just using a BOOL class variable like this:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
hasKeyboardOpen = YES;
}
// your own method to hide keyboard you would call when user physically hide keyboard
- (void)dismissKeyboard
{
[myTextField resignFirstResponder];
hasKeyboardOpen = NO;
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if(hasKeybaordOpen == YES)
{
[myTextfield performSelector:#selector(becomeFirstResponder)
withObject:nil
afterDelay:1.0f];
}
}
So you before the rotation if the keyboard is visible you it and schedule it to present in 1 second.
I guess the problem is that you schedule the keyboard to present even if it is hidden. If that is the case you should check if the keyboard is visible before performSelector: withObject:afterDelay:
if ([myTextField isFirstResponder]) {
[myTextField resignFirstResponder];
[myTextField performSelector:#selector(becomeFirstResponder) withObject:nil afterDelay:1.];
}
Although I really don't think performSelector: withObject:afterDelay: is the proper way of doing it. You might want to use willRotateToInterfaceOrientation:duration: and didRotateFromInterfaceOrientation: methods instead. You can store the reference to the textField or any other firstResponder (i.e. if you have multiple textFields or textViews:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if ([self.myTextFiled isFirstResponder]) {
self.toBeFirstResponderAfterInterfaceOrientationChange = self.myTextFiled;
[self.myTextFiled resignFirstResponder];
}
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
if (self.toBeFirstResponderAfterInterfaceOrientationChange) {
[self.toBeFirstResponderAfterInterfaceOrientationChange becomeFirstResponder];
self.toBeFirstResponderAfterInterfaceOrientationChange = nil;
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
self.toBeFirstResponderAfterInterfaceOrientationChange = nil;
}

Keyboard dismissed as soon as presented

I have an app which involves using different kinds of Gestures. So, in order to differentiate between thse gestures, I used shouldRecognizeSimultaneouslyWithGestureRecognizer method which returns a YES. However, on doing this the keyboard on the UIWebView is dismissed right after it is being presented. And if I don't then the keyboard functions correctly but the controller fails to recognize different gestures.
How do I make the keyboard function correctly and at the same time recognize different gestures ?
Try this one
#pragma mark -
#pragma mark UITextFieldDelegate Methods
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
[textField resignFirstResponder];
}
//You can use like this in your shouldRecognizeSimultaneouslyWithGestureRecognizer method
[textView resignFirstResponder];

Modify CoreData entity attribute value via UITextView

I have UITextView which shows some text data from CoreData entity.
I need the possibility to edit this text data and save changes after keyboard dismissed.
How to do that?
I know how to save data but I don't know how to dismiss keyboard if I tap outside UITextView. May be there is some native way to do that.
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
if ([appDelegate.managedObjectContext save:&error]) {
...
}
Thanks for any help or advice!
First you need to set the delegate for UITextview to current view.We have native code for when we click on return button on keyboard we will get the event using UIKit.Framework, Inside that function write your save or other logics.
Code:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
// ------- here is your logics -------- //
}
textField => Your text field object.
To save data after you stop editing UITextView and have dismissed the keyboard,
use TextView's Delegate method
(void)textViewDidEndEditing:(UITextView *)textView
In this method, save the new text in the Entity's appropriate property and save the managed object context to persist the data.
To dismiss to dismiss keyboard if you tap outside UITextView
(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[TextView resignFirstResponder];
}
To dismiss the keyboard you can use.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.view endEditing:YES];
}

hiding keyboard ios [duplicate]

This question already has answers here:
Dismiss keyboard on touch anywhere outside UITextField
(8 answers)
Closed 9 years ago.
I have a few text inputs and I can hide the keyboard whenever I touch the background, but only when I have been entering into the first text box name textField1. now this code should be simple but I just can't seem to get it.
-(IBAction)backgroundTouched:(id)sender {
[textField1 resignFirstResponder];
[buildLength resignFirstResponder];
[buildWidth resignFirstResponder];
[ridgeWidth resignFirstResponder];
[rafterWidth resignFirstResponder];
[hipWidth resignFirstResponder];
[eaveOverhang resignFirstResponder];
[spacing resignFirstResponder];
}
If you want to hide the keyboard when you tap a button and you have more than one UITextFields in your view, then you should use:
[self.view endEditing:YES];
Tap anywhere on the view, and the keyboard will disappear.
Try this:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[[self view] endEditing:YES];
}
You can also iterate through an array of views (such as your UIView's subviews) and manually resign the keyboard, this is good if you dont want to resign on ALL the subviews within your parent UIView.
- (void)viewDidLoad
{
self.view.userInteractionEnabled = TRUE;
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//Iterate through your subviews, or some other custom array of views
for (UIView *view in self.view.subviews)
[view resignFirstResponder];
}
You can try UITouch method, and in this set your text field object and call resignFirstResponder
when ever you touch on the screen the keyboard will resign, I hope this will work for you.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[currentSelectedTextField resignFirstResponder];
}

Resources