关闭 UIScrollView 中的键盘

发布于 2024-10-19 08:34:15 字数 372 浏览 4 评论 0原文

好吧,我在 UIScrollView 中有几个 UITextFieldsUITextViews,我想将键盘设置为在 UIScrollView 出现时消失。 >scrollview 被触摸或滚动(当然,当您触摸文本字段/视图内部时除外)。

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

预先感谢您的帮助。

Alright, I have a couple of UITextFields and UITextViews inside a UIScrollView, and I'd like to set the keyboard to disappear whenever the scrollview is touched or scrolled (except when you touch down inside the text field/view, of course).

My current attempt at doing this is replacing the UIScrollView with a subclass, and setting it to call a removeKeyboard function (defined inside the main view controller) inside the touchesBegan method. However, this only removes the keyboard for a normal touch, not when the view is simply scrolled. So, what's the best way to remove the keyboard inside a UIScrollView?

Thanks in advance for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

瞄了个咪的 2024-10-26 08:34:15

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

scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;

或者

scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;

用 Swift 表示:

scrollView.keyboardDismissMode = .onDrag

或者

scrollView.keyboardDismissMode = .interactive

Here is the cleanest way to achieve this in iOS 7.0 and above.

scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;

Or

scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;

In Swift:

scrollView.keyboardDismissMode = .onDrag

Or

scrollView.keyboardDismissMode = .interactive
围归者 2024-10-26 08:34:15

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

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

Bit late but if anyone else is searching an answer to this problem, this is how I have gone about solving it:

1) Create a tap gesture recognizer with a target callback method to dismiss your keyboard using resignFirstResponder on all your fields.

2) Add the tap gesture to the scrollview.

Here's an example:

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];
}
落日海湾 2024-10-26 08:34:15

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

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

滚动视图时使键盘消失

然后滚动视图时消失键盘被点击:

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

Although the essence is the same, I prefer less code.

Setting the keyboard to disappear when the scrollView is scrolled in Attributes inspector:

make keyboard disappear when scrollView is scrolled

Then disappear keyboard when scrollView is tapped:

  1. Drag a Tap Gesture Recognizer onto your scrollView
  2. Ctrl-Drag
  3. Make an action
  4. Only one line in the action —— scrollView.endEditing(true). If you are using Objective-C, [self.scrollView endEditing: YES];
找回味觉 2024-10-26 08:34:15

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

In Swift:

Bit late but if anyone else is searching an answer to this problem, this is how I have gone about solving it:

1) Create a tap gesture recognizer with a target callback method to dismiss your keyboard using resignFirstResponder on all your fields.

2) Add the tap gesture to the scrollview.

Here's an example:

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()
    }
}
淡忘如思 2024-10-26 08:34:15

查看 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

Look at keyboardDismissMode property of 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.

Swift Version

vwScrollView.keyboardDismissMode = .interactive
vwScrollView.keyboardDismissMode = .onDrag
几度春秋 2024-10-26 08:34:15

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

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

Create a extension class for hiding keyboard when touches scrollview/view anywhere

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

And call this method in viewDidLoad like

override func viewDidLoad() {
  super.viewDidLoad()
  self.hideKeyboardWhenTappedAround()    
}
等你爱我 2024-10-26 08:34:15

试试这个

[self.selectedViewController.view endEditing:YES];

Try This

[self.selectedViewController.view endEditing:YES];
忘东忘西忘不掉你 2024-10-26 08:34:15

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

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

A bit late but if anyone else is searching an answer to this problem with Swift 3:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    view.endEditing(true)
}
初相遇 2024-10-26 08:34:15

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

DismissKeyboardTapGesture 类使用 ARC,可处理视图下的任何文本字段,并且不会接管来自子视图(如按钮)的任何点击。还利用iOS7的滚动效果来消除键盘。

从 UIScrollView 超类设置:

    _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

When I added the gesture to a subclass of UIScrollView, I was having problems with the various gestures in my view tree interfering with each other, such as being able to click on subviews, scroll the view, and have the keyboard dismiss in all cases. I came up with this solution, which can be setup from a superclass of UIScrollView or from a UIViewController.

The DismissKeyboardTapGesture class uses ARC, works with any text fields under the view, and doesn't take over any clicks from subviews like buttons. Also takes advantage of iOS7 scrolling effect to dismiss keyboard.

Setting up from UISScrollView superclass:

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

or from UIViewController:

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

Here is the 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
倾城月光淡如水﹏ 2024-10-26 08:34:15

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

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

Try this scroll view delegate method -

link delegate in IB to scroll view and then cop this code (modify as per your need).

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

This should work. You can try other delegate methods too like

   -(void)scrollViewDidScroll: (UIScrollView *)scrollView 
{
//do your stuff
}
毅然前行 2024-10-26 08:34:15
scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
国产ˉ祖宗 2024-10-26 08:34:15
    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
        }
    }
}
    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
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文