【问题标题】:Dismissing the keyboard in a UIScrollView关闭 UIScrollView 中的键盘
【发布时间】:2011-07-05 20:36:00
【问题描述】:

好的,我在 UIScrollView 中有几个 UITextFieldsUITextViews,我想将键盘设置为在触摸或滚动 scrollview 时消失(除非你在里面触摸当然是文本字段/视图)。

我目前的尝试是将UIScrollView 替换为子类,并将其设置为在touchesBegan 方法中调用removeKeyboard 函数(在主视图控制器中定义)。但是,这只会在正常触摸时移除键盘,而不是在简单地滚动视图时。那么,在UIScrollView 中移除键盘的最佳方法是什么?

提前感谢您的帮助。

【问题讨论】:

    标签: ios objective-c swift uiscrollview keyboard


    【解决方案1】:

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

    1) 创建一个带有目标回调方法的点击手势识别器,以在您的所有字段上使用 resignFirstResponder 来关闭您的键盘。

    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];
    }
    

    【讨论】:

    • 在带有 UITextFields 和 UIButtons 的 UIScrollView 上工作正常。
    • 你能给我快速的等效代码吗?谢谢
    • 有关 Swift 版本的信息,请参见下面的帖子。
    • 我不明白为什么我们需要使用 TapGesture?故事板本身有一个可用的选项。在键盘中将“不要关闭”更改为“拖动时关闭”。
    【解决方案2】:

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

    在 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
    }
    

    【讨论】:

      【解决方案3】:

      试试这个

      [self.selectedViewController.view endEditing:YES];
      

      【讨论】:

        【解决方案4】:

        当我将手势添加到 UIScrollView 的子类时,我遇到了视图树中的各种手势相互干扰的问题,例如能够单击子视图、滚动视图和使用键盘在所有情况下解雇。我想出了这个解决方案,可以从UIScrollView 的超类或UIViewController 进行设置。

        DismissKeyboardTapGesture 类使用 ARC,适用于视图下的任何文本字段,并且不会接管来自按钮等子视图的任何点击。还利用 iOS7 滚动效果来关闭键盘。

        从 UISScrollView 超类设置:

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

        或来自 UIViewController:

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

        这是类:

        @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
        

        【讨论】:

          【解决方案5】:

          这是在 iOS 7.0 及更高版本中实现此目的的最简洁方法。

          scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
          

          或者

          scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
          

          在斯威夫特中:

          scrollView.keyboardDismissMode = .onDrag
          

          或者

          scrollView.keyboardDismissMode = .interactive
          

          【讨论】:

          • 你能给我快速的等效代码吗?这个剂量似乎很快就起作用了。
          • 在swift中,scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissMode.OnDrag;或 scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissMode.Interactive;
          • 您可以在控件/视图加载后的任何时间设置它。我建议把它放在 viewDidLoad 中,并确保 scrollView 是通过 IB 连接的。甚至你可以在 IB 中设置它。它在属性检查器的滚动视图部分下被命名为键盘属性。
          • OnDrag 表示当您开始拖动滚动视图时关闭键盘。而交互式意味着您开始在滚动视图中进行触摸。
          • 交互式不会在 Swift 中触发。
          【解决方案6】:

          Swift 中:

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

          1) 创建一个带有目标回调方法的点击手势识别器,以在您的所有字段上使用 resignFirstResponder 关闭您的键盘。

          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()
              }
          }
          

          【讨论】:

            【解决方案7】:

            虽然本质相同,但我更喜欢代码少。

            设置在属性检查器中滚动scrollView时键盘消失:

            然后在点击 scrollView 时消失键盘:

            1. 将点击手势识别器拖到您的滚动视图上
            2. 动作中只有一行——scrollView.endEditing(true)。如果你使用的是 Objective-C,[self.scrollView endEditing: YES];

            【讨论】:

            • 仅在键盘“Dismiss on drag”中不需要操作。
            【解决方案8】:

            有点晚了,但是如果其他人正在用 Swift 3 寻找这个问题的答案:

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

            【讨论】:

              【解决方案9】:

              查看 UIScrollView 的 keyboardDismissMode 属性。

              // 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.
              

              斯威夫特版

              vwScrollView.keyboardDismissMode = .interactive
              vwScrollView.keyboardDismissMode = .onDrag
              

              【讨论】:

                【解决方案10】:
                scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
                

                【讨论】:

                  【解决方案11】:

                  创建一个扩展类,用于在触摸任何地方的滚动视图/视图时隐藏键盘

                  extension UIViewController {
                    func hideKeyboardWhenTappedAround() {
                      let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
                      tap.cancelsTouchesInView = false
                      view.addGestureRecognizer(tap)
                    }
                      
                    @objc func dismissKeyboard() {
                      view.endEditing(true)
                    }
                  }
                  

                  并在viewDidLoad中调用这个方法

                  override func viewDidLoad() {
                    super.viewDidLoad()
                    self.hideKeyboardWhenTappedAround()    
                  }
                  

                  【讨论】:

                    【解决方案12】:
                        extension UIView{
                        //Set tag via storyboard 
                        func keyboardDissmissInteractiveMode(_ tag:Int){
                            if let scrollView = self.viewWithTag(tag) as? UIScrollView{
                                scrollView.keyboardDismissMode = .interactive
                            }
                            if let tableview = self.viewWithTag(tag) as? UITableView{
                                tableview.keyboardDismissMode = .interactive
                            }
                        }
                    
                        func keyboardDissmissOnDragMode(_ tag:Int){
                            if let scrollView = self.viewWithTag(tag) as? UIScrollView{
                                scrollView.keyboardDismissMode = .onDrag
                            }
                            if let tableview = self.viewWithTag(tag) as? UITableView{
                                tableview.keyboardDismissMode = .onDrag
                            }
                        }
                    
                    
                        func keyboardDissmissInteractiveMode(_ view:UIView){
                            if let scrollView = view as? UIScrollView{
                                scrollView.keyboardDismissMode = .interactive
                            }
                            if let tableview = view as? UITableView{
                                tableview.keyboardDismissMode = .interactive
                            }
                        }
                    
                        func keyboardDissmissOnDragMode(_ view:UIView){
                            if let scrollView = view as? UIScrollView{
                                scrollView.keyboardDismissMode = .onDrag
                            }
                            if let tableview = view as? UITableView{
                                tableview.keyboardDismissMode = .onDrag
                            }
                        }
                    }
                    

                    【讨论】:

                      猜你喜欢
                      • 2017-06-25
                      • 1970-01-01
                      • 2016-01-23
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 2013-01-08
                      • 1970-01-01
                      相关资源
                      最近更新 更多