I have UIView with few UITextFields. On load I call [textfield becomeFirstResponder] to bring keyboard on the screen. I don't want to dismiss this keyboard until I done with view.
Keyboard dissapear after I "touch" outside any textfield.
I tried to set
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
return NO;
}
however then I can't dismiss keyboard even after is unloaded.
Any hints how to keep keyboard all the time and dissmiss it just before uiview remove?
When viewWillDisappear (or viewDidDisappear) is called, then call resignFirstResponder on your UITextField.
- (void) viewWillDisappear:(BOOL) animated {
[self.textField resignFirstResponder];
}
call this line before dismiss.
view.endEditing(true)
Related
There are multiple textfields in a viewcontroller in which some of them are customised (one tapping those textfield will launch a popover controller, from that user can select the option which will get displayed in tapped textfield).
I have a tap gesture on the view controller for dismissing the keyboard (if it's on the screen).
Keypad gets locked(if it's visible) when I open the popover controller on taping the customised textfield. The keyboard is not getting dismissed even if I tap on the parent view or else on the dismiss button in the keypad.
I have tried this 2 snippets to hide the keyboard, but it's not working
[self.scrollView endEditing:YES];
[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
You can use textfields delegate to prevent it from presenting a keyboard and instead present popover yourself by implementing this textFieldShouldBeginEditing method
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if(textField == myCustomTextField) {
[self openCustomPopover];
return NO;
}
return YES;
}
more on its delegate methods here https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextFieldDelegate_Protocol/index.html#//apple_ref/occ/intfm/UITextFieldDelegate/textFieldShouldBeginEditing:
The following code used to work in iOS6 to hide the keyboard when a view controller was popped off of the navigation stack:
- (void)viewWillDisappear:(BOOL)animated {
[self.view endEditing:YES];
[super viewWillDisappear:animated];
}
However, in iOS7, the [self.view endEditing:YES] line seems to get ignored. I tried the command in other view events (viewDidDisappear, viewWillAppear, and viewDidAppear), and the only one it worked in is viewDidAppear. It seems that once a "pop" is initiated, we lose the ability to hide the keyboard until the view controller is "pushed" back on the stack.
While placing the code in viewDidAppear does work to hide the keyboard, the bad thing is that the keyboard is displayed briefly when the viewController is pushed back on to the navigation stack...pretty unacceptable from a UI perspective.
Has anyone else had success in working around this issue? I would prefer not to have to write my own CANCEL button, but right now, that is the only thing I can think of that will work.
If it's a UITextView set the editable property to NO. I tried this and it hides the keyboard as soon as it's set. I haven't tried it with a UITextField but I'm guessing you'd get the same result with setting the enabled property to NO. If that doesn't work, create a UITextField with userInteractionEnabled set to NO as a background for a transparent UITextView and use the editable property as stated above.
There was a change in iOS 7 where view controllers that are presented as modal forms cannot dismiss the keyboard by default. To fix this, you need to override your view controller's disablesAutomaticKeyboardDismissal method and return NO.
The problem is that somewhere between the time I press the "BACK" button and the time that viewWillDisappear fires, the current text field's canResignFirstResponder is getting set to FALSE, which is preventing the keyboard from hiding. I have not been able to discover anything in my code which could cause this, and I strongly suspect that it could be some kind of iOS 7 bug, as the same code worked for me under iOS 6.
As a workaround, I implemented the following solution. I subclassed UINavigationController and overrode the following method:
- (UIViewController *)popViewControllerAnimated:(BOOL)animated {
[self.topViewController.view endEditing:YES];
return [super popViewControllerAnimated:animated];
}
This caused the keyboard to appropriately disappear when I tapped the Back button to pop the current view controller. Nice sigh of relief that I didn't have to write a bunch of custom Back buttons.
To hide the keyboard when the text field lost focus
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if ([textField isFirstResponder])
[textField resignFirstResponder];
return YES;
}
I tried a workaround. It may not be what you guys are expecting.
If you are using storyboard, You can resign the keyboard in "prepareForSeuge" method.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
[self.view endEditing:YES];
}
It worked fine for me.
Approach given in the below will definitely hide the status bar in iOS7.
- (BOOL)prefersStatusBarHidden
{
return YES;
}
Add this to your .plist file (go to 'info' in your application settings)
View controller-based status bar appearance --- NO
Then you can call this line to hide the status bar:
[[UIApplication sharedApplication] setStatusBarHidden:YES];
In case your app is developed to support iPhone only, status bar won't hide when you run your app in iPad.
Put your UItextfield or UItextview for global declaration.
UITextfield textfield = your textfield object;
-(void)viewWillDisappear:(BOOL)animated
{
[self.view endEditing:YES];
[textfield resignFirstResponder];
[super viewWillDisappear:animated];
}
I have a view controller whose view is setup in such a way that it has 3 buttons and other subviews. On clicking one of the buttons(3rd button), i am adding another view controller's view as subview to self.view (in this view i have a search display controller in the active state with keyboard)
I'm able to achieve this using the following code
[self.searchDisplayController setActive:YES];
[self.searchDisplayController.searchBar setShowsCancelButton:NO];
[self.searchDisplayController.searchBar becomeFirstResponder];
Now when i press the 2nd button, i try removing this view from the superview and also try to resign the keyboard in the viewWillDisappear ([self.view endEditing:YES]) in the following manner, but keyboard still doesnt resign
One small edit, it is resigning in case i comment out the following piece of code
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
if (self.dataSource.count)
return YES;
return NO;
}
Try to resign UISearchBar in viewWillDisappear
[yourSearchBar resignFirstResponder];
Inside the method that is called to your 2nd button, just type in
[self.searchDisplayController.searchBar resignFirstResponder];
Leave the viewWillDisappear alone. That is for when the view is done with whatever animation it is doing, and when the view is leaving. If you set the display controller to resigning the responder, it should make it go away immediately.
Hope this helps!
you can do it using NSNotificationCenter as below.
//.m file:
[[NSNotificationCenter defaultCenter]addObserver:self
selector:#selector(ResignFirstResponder:) name:#"resign" object:nil];
in ResignFirstResponder:
[searchBar resignFirstResponder];
in which class you use above addObserver, you have to implement that method.
//.m file from where you want to send action, call method as below. [on your second button click]
[[NSNotificationCenter defaultCenter] postNotificationName:#"resign"
object:nil];
I got a simple detail view with a textfield.
In detail's viewController I wrote this code:
- (void) viewDidAppear:(BOOL)animated{
[self.textField becomeFirstResponder];
NSLog(#"is textField 1st responder %d", [self.textField isFirstResponder]);
}
When I push the detail into a navigation controller, I see "is textField 1st responder 0" in my log.
The keyboard doesn't appear.
But the textfield has blinking cursor.
What happens in this moment? Why the keyboard doesn't appears? I did try to catch a notification "KeyboardDidShown" in my AppDelegate but didn't catch anything:
Here is my master view design :
Does calling reloadInputViews help?
My keyboard appears with a textView, I want to hide it when the user push on a back button on a navigation bar.
I have tried this:
-(void)viewWillDisappear:(BOOL)animated{
[myTextView resignFirstResponder];
}
and this:
-(void)viewDidDisappear:(BOOL)animated{
[myTextView resignFirstResponder];
}
But it doesn't work, how can I do this?
edit:
I found the solution here:
iPad keyboard will not dismiss if modal ViewController presentation style is UIModalPresentationFormSheet
Put this into the buttonPress method -
[self.view.window endEditing:YES];
Edit - this also lets you get the contents of the text being edited when the "back" button is pressed
Combining the above answers and checking for back button will be done by this
- (void)viewWillDisappear:(BOOL)animated{
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
// back button was pressed. We know this is true because self is no longer
// in the navigation stack.
[self.view.window endEditing:YES];
}
[super viewWillDisappear:animated];
}