源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Swift让输入框跟随键盘弹起避免输入输入法挡住输入框问题

  • 时间:2021-12-18 06:45 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Swift让输入框跟随键盘弹起避免输入输入法挡住输入框问题
[b]第一步: 新建Controller[/b] 在Xcode选择File → New → File → Cocoa Touch Class 新建LoginViewController继承自UIViewController [b]第二步:创建两个UITextField[/b] passwordInput: UITextField // 密码输入框 accountInput: UITextField // 帐号输入框 [b]第三步:添加键盘KVO[/b] 在viewDidLoad方法添加下面两行代码
//当键盘弹起的时候会向系统发出一个通知,
//这个时候需要注册一个监听器响应该通知
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(LoginViewController.keyboardWillShow(_:)), name:UIKeyboardWillShowNotification, object: nil)
//当键盘收起的时候会向系统发出一个通知,
//这个时候需要注册另外一个监听器响应该通知
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(LoginViewController.keyboardWillHide(_:)), name:UIKeyboardWillHideNotification, object: nil)
[b]添加全局控制参数[/b] 因为连续在两个或多个textfield之间切换时候,只会发送UIKeyboardWillShowNotification键盘显示通知,而不会发送UIKeyboardWillHideNotification键盘隐藏通知,这就需要一个全局参数控制键盘只在第一次点击输入框时候界面上移,该参数变为false,光标移到另一个输入框时界面不再变化。当关闭键盘时候,界面下移,并将这个参数恢复为默认值。 在类的第一行声明该变量:
var keyBoardNeedLayout: Bool = true
[b]添加两个方法分别相应键盘弹起和键盘隐藏[/b] [b]键盘弹起响应[/b]
func keyboardWillShow(notification: NSNotification) {
print("show")
if let userInfo = notification.userInfo,
value = userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue,
duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? Double,
curve = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? UInt {
let frame = value.CGRectValue()
let intersection = CGRectIntersection(frame, self.view.frame)
let deltaY = CGRectGetHeight(intersection)
if keyBoardNeedLayout {
UIView.animateWithDuration(duration, delay: 0.0,
options: UIViewAnimationOptions(rawValue: curve),
animations: { _ in
self.view.frame = CGRectMake(0,-deltaY,self.view.bounds.width,self.view.bounds.height)
self.keyBoardNeedLayout = false
self.view.layoutIfNeeded()
}, completion: nil)
}
}
}
[b]键盘隐藏响应[/b]
func keyboardWillHide(notification: NSNotification) {
print("hide")
if let userInfo = notification.userInfo,
value = userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue,
duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? Double,
curve = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? UInt {
let frame = value.CGRectValue()
let intersection = CGRectIntersection(frame, self.view.frame)
let deltaY = CGRectGetHeight(intersection)
UIView.animateWithDuration(duration, delay: 0.0,
options: UIViewAnimationOptions(rawValue: curve),
animations: { _ in
self.view.frame = CGRectMake(0,deltaY,self.view.bounds.width,self.view.bounds.height)
self.keyBoardNeedLayout = true
self.view.layoutIfNeeded()
}, completion: nil)
}
}
更进一步 如果输入框吸底,y的位移可以用-deltaY
self.view.frame = CGRectMake(0,-deltaY,self.view.bounds.width,self.view.bounds.height)
但是如果输入框在偏上的位置就有可能导致某个输入框移出界面视界,这时候可以把位移写成deltaY/2或者deltaY/4等,自己去尝试吧。 以上所述是小编给大家介绍的Swift让输入框跟随键盘弹起避免输入输入法挡住输入框问题,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程素材网网站的支持!
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部