如何在ScrollView触摸事件中隐藏键盘...
情景就是这样......
- >查看 - > ScrollView - > Textfield
我想在触摸scrollView时隐藏键盘。我试图覆盖scrollview的类,但我仍然无法做到。
答案 0 :(得分:8)
这样做会有所帮助:
@interface MyClass <UIScrollViewDelegate> {
}
@implementation
- (void)viewDidLoad {
yourScrollView.delegate = self;
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
[myTextField resignFirstResponder];
}
如果你真的想要处理触摸事件,那么你需要子类化UIScrollView并覆盖方法:
- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view {
}
的更多信息
答案 1 :(得分:6)
这对我有用
UITapGestureRecognizer *tapScroll = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapped)];
tapScroll.cancelsTouchesInView = NO;
[viewScroller addGestureRecognizer:tapScroll];
其中viewScroller是您的滚动条。在我们的tapped方法中
- (void) tapped {
[self.view endEditing:YES];
}
不知道为什么,但上述内容对我不起作用......即使它应该
答案 2 :(得分:4)
试试这个:
为scrollview添加手势识别器,
UITapGestureRecognizer *tapScroll = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped)];
tapScroll.cancelsTouchesInView = NO;
[yourScrollview addGestureRecognizer:tapScroll];
[tapScroll release];
将键盘重新设置为(tapped :)方法。
答案 3 :(得分:0)
请看一下这个答案,这是我找到的最简单的答案。
UitextField resignFirstResponder does not work in scroll view
答案 4 :(得分:0)
- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view
{
[self.view endEditing:YES];
return YES;
}