滚动UITableView时隐藏键盘

ole*_*egi 124 keyboard scroll uitableview ios

在我的应用程序中,当我开始滚动UITableView时,我想隐藏键盘.我在互联网上搜索这个,大多数答案是子类化UITableView(http://stackoverflow.com/questions/3499810/tapping-a-uiscrollview-to-hide-the-keyboard).

我做了子类但它不起作用.

#import <UIKit/UIKit.h>

@protocol MyUITableViewDelegate <NSObject>
@optional
- (void)myUITableViewTouchesBegan;
@end

@interface MyUITableView : UITableView <UITableViewDelegate, UIScrollViewDelegate> {
    id<MyUITableViewDelegate> delegate;
}
@end
Run Code Online (Sandbox Code Playgroud)

.m文件

#import "MyUITableView.h"

@implementation MyUITableView

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSLog(@"delegate scrollView"); //this is dont'work
    [super scrollViewDidScroll:scrollView];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"delegate myUITableViewTouchesBegan"); // work only here
    [delegate myUITableViewTouchesBegan];
    [super touchesBegan:touches withEvent:event];

}

- (void)dealloc {
...
Run Code Online (Sandbox Code Playgroud)

我像这样使用这个类.但委托函数myUITableViewTouchesBegan在ViewController中不起作用

.H

#import <UIKit/UIKit.h>
#import "MyUITableView.h"

@interface FirstViewController : UIViewController <UITableViewDelegate, UISearchBarDelegate, MyUITableViewDelegate> {
    MyUITableView *myTableView;
    UISearchBar *searchBar; 
}

@property(nonatomic,retain) IBOutlet MyUITableView *myTableView;
...
Run Code Online (Sandbox Code Playgroud)

.M

- (void) myUITableViewTouchesBegan{
    NSLog(@"myUITableViewTouchesBegan");
    [searchBar resignFirstResponder];
}
Run Code Online (Sandbox Code Playgroud)

我对这个实现有一些麻烦:
1)myUITableViewTouchesBegan在ViewController中不起作用
2)来自MyUITableView.m的NSLog - NSLog(@"delegate myUITableViewTouchesBegan"); 只有当我触摸桌子时工作.当我开始滚动时,它是如何工作的?
我尝试覆盖scrollViewDidScroll但是comiler说MyUITableVIew可能不响应这个字符串[super scrollViewDidScroll:scrollView];

Pei*_*Pei 386

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

tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
Run Code Online (Sandbox Code Playgroud)

或触摸时解雇:

tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
Run Code Online (Sandbox Code Playgroud)

或者在Swift中:

tableView.keyboardDismissMode = .onDrag
Run Code Online (Sandbox Code Playgroud)

滚动时以交互方式解除:

tableView.keyboardDismissMode = .interactive
Run Code Online (Sandbox Code Playgroud)

  • 当然,也可以在属性检查器中为使用笔尖和故事板的人设置此属性. (19认同)
  • 只有UIScrollViewKeyboardDismissModeOnDrag才有效. (9认同)
  • 对于那些忘记的人,你仍然需要让UITextfield辞职第一响应者. (2认同)

小智 140

不知道为什么你需要为此子类化UITableView.

在包含普通UITableView的视图控制器中,尝试添加以下内容:

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [searchBar resignFirstResponder];
}
Run Code Online (Sandbox Code Playgroud)


Kyl*_*egg 123

您可以在Interface Builder中执行此操作.选择您的UITableView并打开属性检查器.在"滚动视图"部分中,将" 键盘"字段设置为"拖动时关闭".

在此输入图像描述


Gar*_*nes 40

只是为上面的答案添加更新.以下在Swift 1.2中为我工作

tableView.keyboardDismissMode = UIScrollViewKeyboardDismissMode.OnDrag
Run Code Online (Sandbox Code Playgroud)

要么

tableView.keyboardDismissMode = UIScrollViewKeyboardDismissMode.Interactive
Run Code Online (Sandbox Code Playgroud)


小智 5

使用 Swift 5

要在滚动 TableView 时隐藏键盘并正确停止编辑,我们仍然需要结合两种类型的答案:

  1. 例如,在 IB(如Kyle解释)或ViewDidLoad()代码(如Pei解释)中设置键盘关闭模式:
tableView.keyboardDismissMode = .onDrag
Run Code Online (Sandbox Code Playgroud)
  1. 强制当前文本字段作为第一响应者辞职(如Vasily的回答)。我们只需要在我们的UITableViewController类中添加以下内容
    override func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if !tableView.isDecelerating {
            view.endEditing(true)
        }
    }
Run Code Online (Sandbox Code Playgroud)

  • 如果我没有引用实际的文本字段,这将如何工作?我有 3 个单独的视图,一个视图包含表视图,另一个视图包含文本视图。 (2认同)