I have a popover that has a textfield on it, when I click on the popover background, the keyboard disappears. However when I click around the popover area, the keyboard doesn't disappear.
Is there a way I can get into the delegate that collapses the popover so that I can add the resignfirstresponder command.
Sure! UIPopoverController has a delegate method popoverControllerShouldDismissPopover: in which you can add the resignFirstResponder command.
- (BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController
{
[self.someTextField resignFirstResponder];
return YES;
}
You can call resign responder methods to the popover dismissal delegate like :
(void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
OR
(BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController
Related
I have a login screen,when all UITextField are filled user will tap on UIButton for Sign me in. If user tap on background the keyboard will be hide.
But i want to hide keyBoard when user clicked on UIButton which is sign me in button.When UIButton action TouchUpInside called,at that time i want to hide the keyboard. I do not want to hide keyboard on the tap of anywhere in the view.
Thanks in advance.
Call [yourTextField resignFirstResponder]; in your loginAction
- (IBAction)loginAction:(id)sender {
[yourTextField resignFirstResponder];
// your rest code goes here...
}
Just add this to the method in which you use the button for example:
- (IBAction)yourButtonMethod:(id)sender {
// Your operations
[self.yourTextFieldName resignFirstResponder]
}
Note: You must set delegate to self
Eg:
yourTextField.delegate=self;
Then:
- (IBAction)HideKeyBoard:(id)sender
{
[yourTextField resignFirstResponder];
}
just call resignFirstResponder on you textfield. like
[myTextField resignFirstResponder]; in IBACtion of button.
By default when tapping on a UITextField iOS will display a default keyboard. Is it possible to bypass this? I would like to display modally a custom view controller on tap on the textField and be able to edit the textField through this controller.
Is there a recommended way?
Following wil repalce the keyboard as the input view when the user clicks on the UItextField.
self.TextField.inputView = "your view ";
Ok tried out the exact requirement you asked for:-
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
[textField resignFirstResponder];
POCModalViewController *objPOCModalViewController = [[POCModalViewController alloc]init];
[self presentViewController:objPOCModalViewController animated:YES completion:nil];
return NO;
}
Where POCModalViewController is the controller you want to present.
I would like to post the solution i have finally implemented, which is the closest to Footyapps27 solution:
I have made the controller that will present the modal controller(which will contain internally multiple custom keyboard views) as the uitextfield delegate for any UITextField objects contained within the view of my controller.
I can now received any notification through the - (BOOL)textFieldShouldBeginEditing:(TWValueInput *)textField method when a textfield start to be edited:
Within that delegate method I have the following code snippet:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
MyCustomKeyboardVC* vc = [[UIStoryboard storyboardWithName:#"main" bundle:nil] instantiateViewControllerWithIdentifier:#"customKeyboardController"];
vc.delegate = self;
self.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:vc animated:NO completion:nil];
return NO;
}
returning NO within that method will prevent the default keyboard from being displayed. There is no need actually to call the resignFirstResponderon the textfield.
I should point out though that the Apple recommended way to display a custom keyboard is to provide a custom view to the textfield inputView property like Divya mentioned. Since i wanted to managed multiple keyboard view entries it was quicker for me to display a custom keyboard controller through the delegate method i mentioned above.
This is Screenshot
In View B, When keyboard is shown and pop viewController to View A with interactivePopGestureRecognizer, the keyboard is still stay here :(
How can let keyboard move with view B ?
(like iMessage or Facebook Messenger)
ps: I'm try to get keyboard view and add to self.view, it's useful, but i think it's not a good way.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardDidShown)
name:UIKeyboardDidShowNotification object:nil];
and
- (void)keyboardDidShown
{
UIView * keyboardView = self.textView.inputAccessoryView.superview;
[self.view addSubview: keyboardView];
}
Try this solution:
https://github.com/cotap/TAPKeyboardPop
This is lightweight category that listens interactivePopGestureRecognizer gesture and animating keyboard accordingly.
You don't add keybordView to the view. If you want to show keyboard for your UITextField/UITextView you call becomeFirstResponder on that object. In your example it should be:
[self.textView becomeFirstResponder];
If you want to hide keyboard you call:
[self.textView resignFirstResponder];
And if you want to hide your keyboard before you dismiss your ViewA you should call it in the beginning of your interactivePopGestureRecognizer when you start animation to show ViewB.
Hope it help.
//EXTENDED
I don't know why you are trying to add accessoryView as a subview to your view
- (void)keyboardDidShown
{
UIView * keyboardView = self.textView.inputAccessoryView.superview;
[self.view addSubview: keyboardView];
}
And I don't understand why do you use notification centre.
The functionality that the keyboard stay when you dismissed the view (as iMessage app) is a standard functionality.
Just use
[self.textView becomeFirstResponder];
when you want to show the keyboard and don't call
[self.textView resignFirstResponder];
when you pop your view controller. iOS does the job for you.
I have a UITextField in my iOS app. When a user enters text and clicks Return, the keyboard goes away due to a call to an IBAction with "resignFirstResponder."
However, XCode does not let me drag a line from the UIView itself to File Owner. How do I associate touching the background of a UIView with an IBAction that makes the keyboard go away?
You can use UITapGestureRecognizer. see: Dismiss keyboard by touching background of UITableView
so instead of tableview, just add it to your view instead:
UITapGestureRecognizer *gestureRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(hideKeyboard)] autorelease];
gestureRecognizer.cancelsTouchesInView = NO; //so that action such as clear text field button can be pressed
[self.view addGestureRecognizer:gestureRecognizer];
and have a method to hide your keyboard
- (void) hideKeyboard {
[self.view endEditing:YES];
}
You've already noticed that you can't drag from the UIView to the file's owner to assoctiate an action with a touch.
The way to work around this is to change the class of the background view from UIView to UIControl and hook up an action from there to a method in your controller to stop editing.
That's because a UIControl can respond to touch events, and a UIView does not, but a UIControl subclasses UIView, and so it can be used in place of a UIView.
I wrote an example project a while ago that uses this technique. Have a look at the secondViewController's xib file and see how I've change the class of the background view and hooked it up to a an action in the controller to dismiss the keyboard.
Use the touchesBegan with Event and end editing on the view:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}
One easy way to do it is to create a big transparent UIButton behind the view.
In my app I am using a SplitViewContorller. In portrait mode I don't want the popover disappear when the user touch outside of it. I want keep in front till the user do something. How it's possible to do that?
In your UISplitViewControllerDelegate, implement splitViewController:popoverController:willPresentViewController:. In that method, you should be able to set yourself as the delegate to the UIPopoverController that is about to be displayed. Then, you can simply return NO from popoverControllerShouldDismissPopover:. You will then be responsible for dismissing the UIPopoverController programatically.
OK, here the code that explain the Sebastian answer, just wrote it on DetailViewController.m:
- (void)splitViewController:(UISplitViewController *)svc popoverController:(UIPopoverController *)pc willPresentViewController:(UIViewController *)aViewController {
pc.delegate = self; }
-(BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController {
return NO; }