Use the slide gesture to pop a view controller, and the keyboard disappears after the view controller disappears.
I want it to disappear together with the view controller like iPhone Message's keyboard.
You need to manually hide the keyboard by calling resignFirstResponder on your textfields or whatever it is you are using to get keyboard input.
Or you can use [self.view endEditing:YES];
Whichever one you choose, you can run it in the viewWillDisappear, that way the keyboard will dismiss along with the view. For example:
- (void)viewWillDisappear:(BOOL)animated {
[self.view endEditing:YES]; //Hides keyboard
[super viewWillDisappear:animated];
}
Related
First of all , my project is so structured:
-home page with a tableView to add element
-floating Button: when clicked make appear keyboard with above a custom view (and inside a textView) to digit input
Now, I have a problem with inputAccessoryView and dismiss keyboard:
I have used inputAccessoryView to move a custom view above the keyboard like this:
MytextView.inputAccessoryView= MyContainerView;
And this work correctly.
My problem occur when I dismiss keyboard using:
[MytextView resignFirstResponder];
The keyboard and relative inputView disappear properly but After when I try again to make MytextView the firstResponder does not work (the keyboard not appear).
I hypothesize that occurs because textView is hide with inputAccessoryView under the screen and inputAccessoryView change The inizial position of textview (Initial in the Middle of the screen); so textView is not focusable and keyboard not appear when I use:
[MyTextView becomeFirstResponder]
Is there a way to reposition programmatically the textView to initial position (middle of the screen) so can become focusable when I call becomeFirstResponder?
Or is there a way to fix inputAccessoryView in the safe area when i dismiss keyboard?
(Sorry , I’m New to objective-c and IOS)
Thanks!
Let me jump out of the comments to have more freedom. Based on your description you could do something like this
- (void) viewDidLoad
{
super.viewDidLoad;
[NSNotificationCenter.defaultCenter addObserver:self
selector:#selector( keyboardDidHide: )
name:UIKeyboardDidHideNotification
object:nil];
}
- ( void ) keyboardDidHide:( NSNotification * ) notification
{
... do stuff here...
}
Then you can reposition or do whatever you need in there. Alternatively, you can also listen for UIKeyboardDidShowNotification and do preparatory stuff there. In fact, there is a whole family of UIKeyboard... notifications that you could use this way and I hope it helps.
EDIT
Here is something else, if you ever need to reposition based on the keyboard size.
- ( void ) keyboardDidShow:(NSNotification *)notification
{
NSDictionary * info = notification.userInfo;
CGRect kbRect = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
... do stuff with kbRect ...
}
have you tried to use
[self.view endEditing:YES]
to dismiss the input view?
I never get any issue to set any input to firstResponder after I trigger that code.
I need to hide the IOS keyboard when i press a button on the screen.
Whatever i try, the keyboard doesn't go away when i press the button on the screen.
// This is the button
-(IBAction)showDateView:(id)sender
// hide keyboard
[self.view endEditing:YES];
}
Thanks
[self.textfield resignFirstResponder];
- resignFirstResponder
Notifies the receiver that it has been asked to relinquish its status as first responder in its window.
in general there may be more than one textfields in your screen you don't know which textField must be resigned so add all the textField objects into an array and iterate a loop to resignFirstResponder
for (uilabel *textField in labelObjArray) {
[textField/textView resignFirstResponder]
}
the key board will be resigned immediate
[self.view endEditing:YES];
This will work if the responder was initiated from the view.
[self.navigationController.navigationBar endEditing:YES]
This will work if the responder was initiated from the navigation bar.
Hi, I am not able to dismiss keyboard. I have a scroll view which have UITextFields on a it. I tried using
[self.view endEditing:YES];
And
[self.scrollView endEditing:YES];
I tried using resignFirstResponder on individual textfields but no use.
This issue only occurs when I tap a textfield which I am using as button when taped upon it I use
[textField resignFirstResponder];
but old one don't resign whatever I do like I tried using endEditing before I resigned the button like textfield. So my question is what could be the problem in my scenario and is there any way to forcefully dismiss keyboard?
If you want textField to act as button, use delegates.
textField.delegate=self;
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return NO;
}
I have created in storyboard simple app (only one controller), I put scrollview and inside scrollview couple UITextFileds. Inside controller I have added function like
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.name resignFirstResponder];
[self.number resignFirstResponder];
// I have tried with and without this line but doesn't work
[self.scrollView resignFirstResponder];
}
(name, number are Outlets of UITextField, scrollView is Outlet of UIScrollView). When I click on any of those text fields keyboard pops up but when I finish typing I cannot hide keyboard.
(In previous version I didn't have scrollview and keyboard hides when I click out the text field). How to make to keyboard has behavior like in default apps, how to hide ?
I'm assuming you want to just be able to tap away from the keyboard and have it dismissed right? Just do this:
UITapGestureRecognizer *myTapz = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(userTapped)];
myTapz.numberOfTapsRequired=1;
myTapz.cancelsTouchesInView=NO;
[self.view addGestureRecognizer:myTapz];//or do self.WhateverYourOtherViewIsCalled..tableview? scrollView?
[myTapz release];
And then in your selector:
-(IBAction)userTapped
{
[whateverYourTextFieldIsCalled resignFirstResponder];
}
In your view controller:
[self.view endEditing:YES];
This will dismiss the keyboard no matter what field is the first responder. I think there are some exceptions, but for what you're doing it should work fine.
Also touchesBegan is a UIView method, not a UIViewController method. If you're putting it inside your UIScrollView, the scroll view's panGestureRecognizer is going to prevent touchesBegan from being called. Also when overriding touchesBegan, or other touches methods, you typically want to call super as well.
ttarules's suggestion for creating a gesture recognizer is the best way for detecting touches. You can use touchesBegan inside the view, just know that other gesture recognizers can prevent it from being called (see Session 121 - Advanced Gesture Recognition from WWDC 2010).
endEditing is the best way to dismiss the keyboard because it works even after you add other fields.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Easy way to dismiss keyboard?
In view1, the keyboard gets shown. I want to dismiss the keyboard when i move to another view (view2).
In view2, in the method that gets called when the view is shown, i try to do so:
[self endEditing:YES];
But this doesn't work, so i get the idea of catching the event (in View1) of a hidden view and dismiss the keyboard before moving to view2. Is this possible?
EDIT:
I think i need to clarify that view2 is not fully hidden when view1 is shown. It's 50 shown vertically.
As long as i work on view1, the Keyboard is shown and view2 is also shown (50%). view2 has a button, when i click on that button, i need to dismiss the keyboard (which is shown from view1).
I tried to mplement a method in view1 like so:
-(void)dismissKeyBoard{
[self endEditing:YES];
}
And call the method above in view2 when i click on the button but it doesn't work.
PS: The button i click in view2 will make view2 shows fully (100%) above view1.
To dismiss the keyboard, you just have to "resignFirstResponder" on the input field that it is currently in. One easy way is to have a catch all method of all the input fields in your view.
So for example, I usually create a method like this:
-(void)dismissKeyboard {
[self.textfield1 resignFirstResponder];
[self.textfield2 resignFirstResponder];
}
then just call it before you transition
[self dismissKeyboard]