如何在触摸UITextField(swift)之外的任何位置时closures键盘?
我正在一个项目,有一个UIViewController,在视图控制器上有一个UIScrollView和UITextField滚动视图。 喜欢这个: 我试图解雇键盘,并在文本框中input一些文本后点击隐藏,然后点击文本框外的任何地方。 我试过下面的代码:
override func viewDidLoad() { super.viewDidLoad() self.textField.delegate = self; } override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { self.view.endEditing(true) }
当我点击滚动视图外时,它适用于我,但是当我点击滚动视图时,什么也没有发生,键盘也不会隐藏。
在文本区以外的任何地方轻击键盘时是否有任何方法解除键盘? 谢谢
编辑Swift 4
编辑:添加@objc
。 虽然这不是性能的最佳select,但在这里有一个例子不应该导致太多的问题,直到有更好的解决scheme。
这应该有助于你有多个UITextView
或UITextField
创build视图控制器的扩展。 这对我来说工作起来更顺畅,而且比尝试使用.resignFirstResponder()
extension UIViewController { func hideKeyboard() { let tap: UITapGestureRecognizer = UITapGestureRecognizer( target: self, action: #selector(UIViewController.dismissKeyboard)) view.addGestureRecognizer(tap) } @objc func dismissKeyboard() { view.endEditing(true) } }
在viewDidLoad中调用self.hideKeyboard()
试试这个,testing和工作:
对于Swift 3.0 / 4.0
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { self.view.endEditing(true) }
老斯威夫特
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent) { self.view.endEditing(true) }
在这种情况下,UITapGesture就是其中一个select。 我试图创build示例代码以防万一。 喜欢这个,
class ViewController: UIViewController { @IBOutlet weak var textField: UITextField! @IBOutlet weak var scrollView: UIScrollView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let tapGesture = UITapGestureRecognizer(target: self, action: "tap:") view.addGestureRecognizer(tapGesture) } func tap(gesture: UITapGestureRecognizer) { textField.resignFirstResponder() } }
适用于Swift 3的工作解决scheme,适用于ScrollView
class ViewController: UIViewController { @IBOutlet weak var textField: UITextField! @IBOutlet weak var scrollView: UIScrollView! override func viewDidLoad() { super.viewDidLoad() // The next line is the crucial part // The action is where Swift 3 varies from previous versions let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.tap(gesture:))) self.view.addGestureRecognizer(tapGesture) } func tap(gesture: UITapGestureRecognizer) { textField.resignFirstResponder() } }
另外一个问题是关于我引用和使用的这个问题。 接受的答案不再适用于Swift 3.当前select的答案应该是下面的答案。
细节
xCode 8.2.1,Swift 3
任务
为UIViewController的视图设置UITapGestureRecognizer,该视图将closures键盘,并将此function作为UIView的Bool属性。
复杂的实现,但用法是在一行
类TapGestureRecognizer
import UIKit class TapGestureRecognizer: UITapGestureRecognizer { let identifier: String private override init(target: Any?, action: Selector?) { self.identifier = "" super.init(target: target, action: action) } init(target: Any?, action: Selector?, identifier: String) { self.identifier = identifier super.init(target: target, action: action) } static func == (left: TapGestureRecognizer, right: TapGestureRecognizer) -> Bool { return left.identifier == right.identifier } }
扩展UIView
import UIKit extension UIView { private var disableKeybordWhenTappedGestureRecognizerIdentifier:String { return "disableKeybordWhenTapped" } private var disableKeybordWhenTappedGestureRecognizer: TapGestureRecognizer? { let hideKeyboardGesture = TapGestureRecognizer(target: self, action: #selector(UIView.hideKeyboard), identifier: disableKeybordWhenTappedGestureRecognizerIdentifier) if let gestureRecognizers = self.gestureRecognizers { for gestureRecognizer in gestureRecognizers { if let tapGestureRecognizer = gestureRecognizer as? TapGestureRecognizer, tapGestureRecognizer == hideKeyboardGesture, tapGestureRecognizer == hideKeyboardGesture { return tapGestureRecognizer } } } return nil } @objc private func hideKeyboard() { endEditing(true) } var disableKeybordWhenTapped: Bool { set { let hideKeyboardGesture = TapGestureRecognizer(target: self, action: #selector(UIView.hideKeyboard), identifier: disableKeybordWhenTappedGestureRecognizerIdentifier) if let disableKeybordWhenTappedGestureRecognizer = self.disableKeybordWhenTappedGestureRecognizer { removeGestureRecognizer(disableKeybordWhenTappedGestureRecognizer) if gestureRecognizers?.count == 0 { gestureRecognizers = nil } } if newValue { addGestureRecognizer(hideKeyboardGesture) } } get { return disableKeybordWhenTappedGestureRecognizer == nil ? false : true } } }
用法
view.disableKeybordWhenTapped = true
完整的示例
import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let textField = UITextField(frame: CGRect(x: 50, y: 50, width: 200, height: 30)) textField.borderStyle = .roundedRect textField.placeholder = "Enter text" view.addSubview(textField) view.disableKeybordWhenTapped = true } }
迅速3
override func viewDidLoad() { super.viewDidLoad() self.view.addGestureRecognizer(UITapGestureRecognizer(target: self.view, action: #selector(UIView.endEditing(_:)))) }
看一下这个。
override func viewDidLoad() { var tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.handleTap)) self.view.userInteractionEnabled = true self.view.addGestureRecognizer(tapGesture) }
然后你的抽头处理程序是。
func handleTap(sender: UITapGestureRecognizer) { self.view.endEditing(true) }
当任何数量的input项目被触摸到外部input区域时,这是有效的。
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { self.view.endEditing(true) }
我有同样的问题,我终于解决了!
在Storyboard中设置TapGestureRecognizer,然后在ViewController中设置Outlet
@IBOutlet var tapGesture: UITapGestureRecognizer!
然后在您的ViewController中设置一个IBAction
@IBAction func DismissKeyboard(sender: UITapGestureRecognizer) { self.view.endEditing(true) }
将这些行添加到您的viewDidLoad方法
override func viewDidLoad() { super.viewDidLoad() self.view.addGestureRecognizer(tapGesture) }
它应该工作
希望这会有所帮助!
我在Obj-C中创build了这个方法,无论用户当前正在input什么内容,都会隐藏键盘:
//call this method + (void)hideKeyboard { //grab the main window of the application UIWindow *window = [UIApplication sharedApplication].keyWindow; //call our recursive method below [self resignResponderForView:window]; } //our recursive method + (void)resignResponderForView:(UIView *)view { //resign responder from this view //If it has the keyboard, then it will hide the keyboard [view resignFirstResponder]; //if it has no subviews, then return back up the stack if (view.subviews.count == 0) return; //go through all of its subviews for (UIView *subview in view.subviews) { //recursively call the method on those subviews [self resignResponderForView:subview]; } }
我希望这可以翻译成Swift并且是合理的。 它可以在应用程序的任何地方调用,并会隐藏键盘,无论你在什么VC或任何东西。
去键盘types和select默认或任何你需要的TextField。 然后重写一个方法调用它,无论你想要什么,我通常称之为“开始”。 以下是您忘记添加的内容
super.touchingBegins(touches, withEvent: event) }
每个触摸不同的文本字段 resignfirstresponder
键盘或使用resignfirstresponder
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; if(![touch.view isMemberOfClass:[UITextField class]]) { [touch.view endEditing:YES]; } }
引入轻敲手势识别器并为其设置和操作。
使用代码:
nameofyourtextfield .resignfirstresponder()