问题
I am using SWRevealViewController in my app and I am having a problem. I have a textfield in a scene, if I swipe left when the keyboard is open, the menu appears but it does not dismiss the keyboard. How do I dismiss keyboard on left swipe?
I have tried
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
swipe.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeRecognizer];
-(void)dismissKeyboard
{
[self.textField resignFirstResponder];
}
but it does not work, I think because I am already using a panGestureRecognizer for revealViewcontroller i.e. [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
I am also using UITapGestureRecognizer
but it only works for tap not for swipe.
回答1:
i think u need to use one of the delegate method in the app delegate there are so may delegate methods are there but u need do somthing like below
dont add any gestures
use this delegate in the appDelegate
delete all the macros begins with #if
don`t need that
put a break-point in app delegate to this method
below delegate method celled each time SWRevealViewController
moved or sliced ..
- (void)revealController:(SWRevealViewController *)revealController didMoveToPosition:(FrontViewPosition)position
{
// NSLog( @"%@: %@", NSStringFromSelector(_cmd), [self stringFromFrontViewPosition:position]);
if(position == FrontViewPositionRight) //check the where to move
{
UINavigationController *viewController = revealController.frontViewController;
if([viewController.visibleViewController isKindOfClass:[FrontViewController class]])
{
[(FrontViewController *)viewController.visibleViewController dismissKeyboard]; //where this is the method declared in the FrontViewController.h file
}
}
}
there is one warning still it works put break point and check hope this helps u ...
in FrontViewController.h
-(void)dismissKeyboard; //add this
in the FrontViewController.m
-(void)dismissKeyboard
{
if([self.textField isFirstResponder]) //check
[self.textField resignFirstResponder];
}
回答2:
I had the same problem.
I added self.revealController.delegate = self;
to my View Controller which I used as Front View.And delegate method get called.
I used
- (void)revealController:(SWRevealViewController *)revealController willMoveToPosition:(FrontViewPosition)position{}
delegate method and I wrote [textField endEditing:YES];
in this delegate method.
Also added <SWRevealViewControllerDelegate>
to my ViewController.h which is my Front View.
回答3:
Try this:
Please add the below code in SWRevealViewContoller.m in the method -(BOOL)_panGestureShouldBegin
[self.view endEditing:YES];
回答4:
First of all you need to replace the revealToggle: method by your custom method like this:
UIBarButtonItem *barBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"reveal-icon.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(myMethod)];
self.navigationItem.leftBarButtonItem = barBtn;
And than in your method:
-(void)myMethod{
[self.view endEditing:YES];
SWRevealViewController *reveal = self.revealViewController;
[reveal revealToggleAnimated:YES];
}
It will surely work.
回答5:
Try This
[[[self revealViewController] view] endEditing:YES]
Add this in
- (void)revealController:(SWRevealViewController *)revealController didMoveToPosition:(FrontViewPosition)position
or any other delegate method based on your requirement.
回答6:
check current position of frontview and write code of hiding keyboard in if condition.
回答7:
Are you sure that self.textField
is the current first responder? If you are try to resign the first responder in the UIGestureRecognizer
delegate's method – gestureRecognizerShouldBegin:
Obviously return YES in that function.
回答8:
Edit the below method in
SWRevealViewContoller.m
(IBAction)revealToggle:(id)sender {
[self.view endEditing:YES];
[self revealToggleAnimated:YES];
}
来源:https://stackoverflow.com/questions/23011975/swrevealviewcontroller-dismiss-keyboard-on-swipe