swift 键盘属性与事件

1、键盘的类型

        textField1.keyboardType = UIKeyboardType.default  //系统默认的虚拟键盘
textField1.keyboardType = UIKeyboardType.asciiCapable //显示英文字母的虚拟键盘
textField1.keyboardType = UIKeyboardType.numbersAndPunctuation //显示数字和标点的虚拟键盘
textField1.keyboardType = UIKeyboardType.URL //显示便于输入数字的虚拟键盘
textField1.keyboardType = UIKeyboardType.numberPad //显示便于输入数字的虚拟键盘
textField1.keyboardType = UIKeyboardType.phonePad //显示便于拨号呼叫的虚拟键盘
textField1.keyboardType = UIKeyboardType.namePhonePad //显示便于聊天拨号的虚拟键盘
textField1.keyboardType = UIKeyboardType.emailAddress //显示便于输入Email的虚拟键盘
textField1.keyboardType = UIKeyboardType.decimalPad //显示用于输入数字和小数点的虚拟键盘
textField1.keyboardType = UIKeyboardType.twitter //显示方便些Twitter的虚拟键盘
textField1.keyboardType = UIKeyboardType.webSearch //显示便于在网页上书写的虚拟键盘
textField1.keyboardType = UIKeyboardType.asciiCapableNumberPad //显示便于输入数字的虚拟键盘 只支持iOS10
textField1.keyboardType = UIKeyboardType.alphabet //字母键盘 已弃用

2、键盘的外观

       textField1.keyboardAppearance = .default //默认外观:浅灰色
textField1.keyboardAppearance = .dark //黑色
textField1.keyboardAppearance = .light //亮色,与Default很相似

3、键盘的回车键

        textField1.returnKeyType = .default //默认:灰色按钮,标有Return
textField1.returnKeyType = .continue // 标有Continue的蓝色按钮
textField1.returnKeyType = .done //标有Done的蓝色按钮
textField1.returnKeyType = .emergencyCall //紧急呼叫按钮
textField1.returnKeyType = .go //标有Go的蓝色按钮
textField1.returnKeyType = .google //标有Google的蓝色按钮,用于搜索
textField1.returnKeyType = .join //标有Join的蓝色按钮
textField1.returnKeyType = .next //标有Next的蓝色按钮
textField1.returnKeyType = .route //标有Route的蓝色按钮
textField1.returnKeyType = .search //标有Search的蓝色按钮
textField1.returnKeyType = .send //标有Send的蓝色按钮
textField1.returnKeyType = .yahoo //标有Yahoo!的蓝色按钮,用于搜索

4、键盘的回车键

        textField1.autocorrectionType = .default  //默认
textField1.autocorrectionType = .no //不自动更正
textField1.autocorrectionType = .yes //自动更正

5、键盘的安全性

        textField1.isSecureTextEntry = true //输入密码

6、回收键盘

     //按return回收键盘
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
} //点击空白回收键盘
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}

7、键盘的事件 监听键盘的弹出和消失,避免遮挡输入框

只能说这里坑太多了,是真的太多了,和2.0差别太大,不说了坑太多了,上代码

    //Mark:Delegate
func textFieldDidBeginEditing(_ textField: UITextField) {
//监听键盘弹出通知
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
//监听键盘收回通知
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func textFieldDidEndEditing(_ textField: UITextField) {
self.view.endEditing(true) } //键盘显示
func keyboardWillShow(notification:NSNotification) { let textMaxY = self.textField1!.frame.maxY // 取到输入框的最大的y坐标值 let userinfo: NSDictionary = notification.userInfo! as NSDictionary let nsValue:AnyObject? = userinfo.object(forKey: UIKeyboardFrameEndUserInfoKey) as AnyObject? let keyboardY = nsValue?.cgRectValue.origin.y //取到键盘的y坐标 // 设置动画 let duration = 2.0 UIView.animate(withDuration: duration) { () -> Void in
if (textMaxY > keyboardY!) {
self.view.transform = CGAffineTransform(translationX: 0, y: keyboardY! - textMaxY - 10)
}else{
//view.transform = CGAffineTransformIdentity;线性代数里面讲的矩阵变换,这个是恒等变换当 你改变过一个view.transform属性或者view.layer.transform的时候需要恢复默认状态的话,记得先把他们重置可以使用view.transform = CGAffineTransformIdentity,或者view.layer.transform = CATransform3DIdentity,
self.view.transform = CGAffineTransform.identity
}
}
}
//键盘隐藏
func keyboardWillHide(notification:NSNotification){ UIView.animate(withDuration: 2.0) { () -> Void in
self.view.transform = CGAffineTransform.identity
} }

这里只是简单的写法,在正式的项目中,请各位判断是否需要进行动画操作,当然这并不会影响最终的效果,但是对性能会造成一定的影响

下面贴出他到底有多坑:你搜索出来的获取键盘弹出的高度的结果一定是这样的:

let userinfo: NSDictionary = aNotification.userInfo!

let nsValue = userinfo.objectForKey(UIKeyboardFrameEndUserInfoKey)

let keyboardRec = nsValue?.CGRectValue()

let height = keyboardRec?.size.height

文/其所以然(简书作者)
原文链接:http://www.jianshu.com/p/49edd72e07a5
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。

这是2.0的写法,转换成3.0是这样的

 let textMaxY = self.textField1!.frame.maxY

 let userinfo: NSDictionary = notification.userInfo! as NSDictionary

 let nsValue = userinfo.object(forKey: UIKeyboardFrameEndUserInfoKey)

 let keyboardRec = (nsValue as AnyObject).cgRectValue

 let height = keyboardRec?.size.height

然后你会发现 height一直是nil 对啊 一直是nil啊  ,你怎么操作啊,傻逼了吧。原因到底是什么呢?    。。。。。不知道

或者你搜索出来的结果是这样的 :

  let keyboardH : CGFloat? = (((notification.userInfo![UIKeyboardFrameEndUserInfoKey] as AnyObject).cgRectValue?.size.height))

其实是第一种结果的简写,这样依旧获取不到键盘的高度。

具体这两种写法的区别在哪里导致获取不到键盘的高度的,知道的小伙伴还请分享一下,在下也是3.0的菜鸟

上一篇:memcache 杂记


下一篇:apache环境下禁止某文件夹内运行PHP脚本、禁止访问文件或目录执行权限的设置方法