在UIScrollView中closures键盘

好的,我在UIScrollView有两个UITextFieldsUITextViews ,并且我想设置键盘在触摸或滚动滚动视图时消失(当然,除非是在文本框/视图内部)。

我目前的做法是用一个子类replaceUIScrollView ,并将其设置为在touchesBegan方法内调用removeKeyboard函数(在主视图控制器内定义)。 但是,这仅仅是为了正常的触摸,而不是当视图被简单地滚动时移除键盘。 那么,在UIScrollView删除键盘的最好方法是什么?

在此先感谢您的帮助。

这是在iOS 7.0中最简单的方法。

 scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag; 

要么

 scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive; 

迅速:

 scrollView.keyboardDismissMode = .onDrag 

要么

 scrollView.keyboardDismissMode = .interactive 

有点晚了,但如果其他人正在寻找这个问题的答案,这是我解决它的方法:

1)使用目标callback方法创build一个轻击手势识别器,在所有字段上使用resignFirstResponderclosures键盘。

2)将点击手势添加到滚动视图。

这是一个例子:

 UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)]; // prevents the scroll view from swallowing up the touch event of child buttons tapGesture.cancelsTouchesInView = NO; [pageScrollView addGestureRecognizer:tapGesture]; [tapGesture release]; ... // method to hide keyboard when user taps on a scrollview -(void)hideKeyboard { [myTextFieldInScrollView resignFirstResponder]; } 

虽然本质是一样的,我更喜欢更less的代码。

当scrollView在“属性”检查器中滚动时,将键盘设置为消失:

使scrollView滚动时使键盘消失

然后点击scrollView时消失键盘:

  1. 拖动一个水龙头手势识别器到你的scrollView
  2. 按Ctrl键
  3. 做一个行动
  4. 在动作中只有一行 – scrollView.endEditing(true) 。 如果你正在使用Objective-C, [self.scrollView endEditing: YES];

Swift中

有点晚了,但如果其他人正在寻找这个问题的答案,这是我解决它的方法:

1)使用目标callback方法创build一个轻击手势识别器,在所有字段上使用resignFirstResponderclosures键盘。

2)将点击手势添加到滚动视图。

这是一个例子:

 import UIKit class ViewController: UIViewController { @IBOutlet var t1: UITextField! @IBOutlet var t2: UITextField! @IBOutlet var t3: UITextField! @IBOutlet var t4: UITextField! @IBOutlet var srcScrollView: UIScrollView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let tapGesture: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "hideKeyboard") // prevents the scroll view from swallowing up the touch event of child buttons tapGesture.cancelsTouchesInView = false srcScrollView.addGestureRecognizer(tapGesture) } func hideKeyboard() { t1.resignFirstResponder() t2.resignFirstResponder() t3.resignFirstResponder() t4.resignFirstResponder() } } 

尝试这个

[self.selectedViewController.view endEditing:YES] ;

查看UIScrollView属性的键盘消除模式。

 // will hide keyboard when your text field is about to go beyond the keyboard. vwScrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive; // will hide keyboard instantly once the scroll view started scrolling by user. vwScrollView.keyboardDismissMode = UIScrollViewKeyboardDismissOnDrag; // If you need to hide keyboard on tap of scroll view,consider adding a tap gesture or sub class and override touchesbegan: method. 

乐于帮助!

当我将手势添加到UIScrollView的子类中时,我的视图树中的各种手势出现了问题,例如能够点击子视图,滚动视图以及在所有情况下都解除键盘。 我想出了这个解决scheme,可以从UIScrollView的超类或从UIViewController

DismissKeyboardTapGesture类使用ARC,与视图下的任何文本字段一起使用,并且不会像子button那样接pipe子视图中的任何点击。 还利用iOS7的滚动效果来closures键盘。

从UISScrollView超类设置:

  _dismissKeyboard = [[DismissKeyboardTapGesture alloc] initWithView:self]; 

或从UIViewController:

  _dismissKeyboard = [[DismissKeyboardTapGesture alloc] initWithView:self.view]; 

这是class级:

 @interface DismissKeyboardTapGesture : NSObject <UIGestureRecognizerDelegate> @end @implementation DismissKeyboardTapGesture - (id)initWithView:(UIView *)view { self = [super init]; if (self) { UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)]; singleTap.cancelsTouchesInView = NO; singleTap.delegate = self; [view addGestureRecognizer:singleTap]; if ([view respondsToSelector:@selector(setKeyboardDismissMode:)]) { // Bonus effect to dismiss keyboard by scrolling ((UIScrollView *)view).keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive; } } return self; } - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { // Don't stop any existing gestures in our view from working if (otherGestureRecognizer.view == gestureRecognizer.view) { return YES; } return NO; } - (void)singleTap:(UIGestureRecognizer*)gestureRecognizer { // Close keyboard for any text edit views that are children of the main view [gestureRecognizer.view endEditing:YES]; } @end 

有点迟了,但如果其他人正在用Swift 3search这个问题的答案:

 func scrollViewDidScroll(_ scrollView: UIScrollView) { view.endEditing(true) } 

试试这个滚动视图委托方法 –

链接在IB委托滚动视图,然后警察这个代码(根据您的需要修改)。

 - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { //sample code [challengeABallotComponent.voterNameTextField resignFirstResponder]; [challengeABallotComponent.ballotNumberTextField resignFirstResponder]; [locationInformation.pollingLocation resignFirstResponder]; } 

这应该工作。 你也可以尝试其他委托方法

  -(void)scrollViewDidScroll: (UIScrollView *)scrollView { //do your stuff } 
 scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;