【Swift】- UITextField完成输入后关闭软键盘的几种方法
2024-09-01 00:55:22
总结了以下几种方式,欢迎补充
3,为TextField绑定Did End On Exit事件
1,点击编辑区域以外的地方时关闭(空白处区域绑定Touch Up Inside事件
新建一个项目,打开Main.storyboard,添加一个Text Field,与ViewController建立连接,然后点击空白处,在右边窗口修改Custom Class 的class改为UIControl
然后为UIControl绑定Touch Up Inside事件(只有改为UIControl后才能绑定事件)
ViewController:
import UIKit class ViewController: UIViewController { @IBOutlet var name: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
} override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
} @IBAction func CloseKeyBoard(sender: AnyObject) {
name.resignFirstResponder();
} }
2,点击编辑区域以外的地方时关闭(重写touchesEnded)
只需要在ViewController里重写touchesEnded方法
//触摸事件,当一个或多个手指离开屏幕时触发
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
name.resignFirstResponder();
}
3,点击软键盘右下角的Done/Go/Next...关闭键盘(为TextField绑定Did End On Exit事件)
选择Main.storyboard中的Text Field,按住control拖拉的方式为其绑定Did End On Exit事件
@IBAction func DoneCloseKeyBoard(sender: AnyObject) {
name.resignFirstResponder();
}
最新文章
- DEV word文档转换为pdf文件
- jquery中attr和prop的区别、 什么时候用 attr 什么时候用 prop (转自 芈老头 )
- 基本药目录sop
- python学习笔记三 函数(基础篇)
- 10个很有用的高级Git命令
- 用JavaScript(js)对时间格式化
- CoffeeScript NgComponent
- linux下利用curl监控web应用状态
- BinarySearchTree-二叉搜索树
- java排序算法(八):希尔排序(shell排序)
- 深入解析ThreadLocal 详解、实现原理、使用场景方法以及内存泄漏防范 多线程中篇(十七)
- 30分钟了解Springboot整合Shiro
- Fiddler忽略捕捉大文件流
- NOIP2016 巨凉无比的感言
- 自定义安装visual studio 2010开发asp.net
- 【May Be DNK】JSON.parse() and JSON.stringify()的两个实用技巧
- ASP.NET Core 与 .NET Core 演变与基础概述
- MAC安装最新datagrip之后无法非官方激活,而且启动过慢
- Azure ServiceBus的消息中带有@strin3http//schemas.microsoft.com/2003/10/Serialization/�
- 初涉RxAndroid结合Glide实现多图片载入操作
热门文章
- 20162326 齐力锋 2017-2018学期 Bag类的补写博客
- 2018-2019-2 20165114《网络对抗技术》Exp4 恶意代码分析
- Cisco、HUAWEI、H3c、Firewall等设备配置snmp
- 详细解读ARM寄存器之CPSR【转】
- [nowcoder]再编号
- 对拷 使用scp命令在两台linux上对拷文件或者文件夹
- VMware Workstation 12 增加磁盘容量 Windows Server 2012 系统 扩展
- windows系统下载地址大全&;大白菜下载和教程
- LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY
- tsunami:一种基于UDP协议的快速传输