我想知道如何使键盘消失时,用户触摸以外的UITextField。
当前回答
最简单和最短的方法之一是将此代码添加到viewDidLoad
[self.view addGestureRecognizer:[[UITapGestureRecognizer alloc]
initWithTarget:self.view
action:@selector(endEditing:)]];
其他回答
斯威夫特4 oneliner
view.addGestureRecognizer(UITapGestureRecognizer(target: view, action: #selector(UIView.endEditing(_:))))
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first{
view.endEditing(true)
}
}
在这种情况下,可以使用ScrollView,并添加到文本字段在ScrollView和我想点击ScrollView和视图,然后解散键盘。为了以防万一,我尝试创建了示例代码。像这样,
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.tap(_:)))
view.addGestureRecognizer(tapGesture)
// Do any additional setup after loading the view, typically from a nib.
}
func tap(gesture: UITapGestureRecognizer) {
textField.resignFirstResponder()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
你的故事板看,就像。
这个怎么样:我知道这是一个老帖子。它可能会帮助某些人:)
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSArray *subviews = [self.view subviews];
for (id objects in subviews) {
if ([objects isKindOfClass:[UITextField class]]) {
UITextField *theTextField = objects;
if ([objects isFirstResponder]) {
[theTextField resignFirstResponder];
}
}
}
}
有一件事很重要……
添加手势识别到视图可以覆盖它的子视图。 所以只执行一个动作。 如果你想通过父视图和子视图执行多重识别操作… 你必须使用
tap.cancelsTouchesInView = false
完整代码如下:
let tap = UITapGestureRecognizer(target: self, action: #selector(self.superViewClicked))
tap.numberOfTapsRequired = 1
tap.delegate = self
tap.cancelsTouchesInView = false
self.view.addGestureRecognizer(tap)
@objc func superViewClicked(){
self.searchTF.resignFirstResponder()
}
在外面点击关闭软键盘的最佳解决方案是什么?
我将解释一个条件:
想想看:你在顶部有UISearchBar, 然后添加头表视图!
解决方法很简单:
跟踪软键盘打开或关闭 添加手势识别头视图 实现表视图didSelectRowAt的委托
完整的代码:
var isSoftKeyboardActive = false
override func viewDidLoad() {
var tap = UITapGestureRecognizer(target: self, action: #selector(self.tableHeaderClick))
tap.numberOfTapsRequired = 1
tap.delegate = self
tap.cancelsTouchesInView = false
self.headerOfTableView.addGestureRecognizer(tap)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillDisappear), name: UIResponder.keyboardWillHideNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillAppear), name: UIResponder.keyboardWillShowNotification, object: nil)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self)
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if isSoftKeyboardActive == true{
self.searchTF.resignFirstResponder()
}else{
selectedRow = indexPath.row
performSegue(withIdentifier: "segueWebview", sender: self)
}
}
@objc func tableHeaderClick(){
self.searchTF.resignFirstResponder()
}
@objc func keyboardWillAppear() {
isSoftKeyboardActive = true
}
@objc func keyboardWillDisappear() {
isSoftKeyboardActive = false
}
它是如何工作的:
如果软键盘是打开的,用户点击头软键盘关闭 如果软键盘打开,用户点击单元格,键盘关闭,不执行单元格点击
推荐文章
- 如何在iOS上进行base64编码?
- 如何停止不必要的UIButton动画标题变化?
- 是否有可能更新一个本地化的故事板的字符串?
- 以编程方式获取Bundle Identifier
- 为iPad和iPhone设计输入按钮
- 如何在我的iPhone应用程序中使用NSError ?
- 我的预发行应用已经在iTunes Connect中“处理”了一周多,这是怎么回事?
- Xcode iOS项目只显示“我的Mac 64位”,但不显示模拟器或设备
- Objective-C中的自动引用计数不能防止或减少什么样的泄漏?
- 在Xcode 9中如何从打开的多个模拟器中退出或关闭单个模拟器?
- 如何使用Swift播放声音?
- UINavigationBar自定义返回按钮没有标题
- 如何精确地以毫秒为单位记录方法的执行时间?
- 在整个UIWindow中获取UIView的位置
- 如何解散ViewController在Swift?