好吧,我在UITextFields
内有几个UITextViews
和UIScrollView
,我想在触摸scrollview
时将键盘设置为消失或滚动(当你在文本字段/视图中触摸时,当然除外)。
我目前尝试这样做是将UIScrollView
替换为子类,并将其设置为在 touchesBegan 方法内调用removeKeyboard函数(在主视图控制器中定义)。但是,这只会移除键盘以进行正常触摸,而不是仅在滚动视图时。那么,在UIScrollView
内删除键盘的最佳方法是什么?
提前感谢您的帮助。
答案 0 :(得分:131)
这是在iOS 7.0及更高版本中实现此目的的最简洁方法。
scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
或者
scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
夫特:
scrollView.keyboardDismissMode = .onDrag
或者
scrollView.keyboardDismissMode = .interactive
答案 1 :(得分:51)
有点迟,但如果有人正在寻找这个问题的答案,这就是我解决它的方法:
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];
}
答案 2 :(得分:34)
虽然本质是相同的,但我更喜欢代码。
在“属性”检查器中滚动scrollView时,将键盘设置为消失:
点击scrollView时,键盘会消失:
scrollView.endEditing(true)
。如果您使用的是Objective-C,[self.scrollView endEditing: YES];
答案 3 :(得分:10)
在 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()
}
}
答案 4 :(得分:6)
查看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
答案 5 :(得分:3)
试试这个
[self.selectedViewController.view endEditing:YES]
;
答案 6 :(得分:2)
当我将手势添加到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
答案 7 :(得分:2)
有点晚了但是如果有人用 Swift 3 来搜索这个问题的答案:
fileprivate func < <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
switch (lhs, rhs) {
case let (l?, r?):
return l < r
case (nil, _?):
return true
default:
return false
}
}
fileprivate func > <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
switch (lhs, rhs) {
case let (l?, r?):
return l > r
default:
return rhs < lhs
}
}
答案 8 :(得分:2)
创建一个扩展类,用于在任何地方触摸scrollview / view时隐藏键盘
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()
}
答案 9 :(得分:0)
试试这个滚动视图委托方法 -
将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
}
答案 10 :(得分:0)
scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
答案 11 :(得分:0)
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
}
}
}