如何在键盘存在时使 UIView 向上移动
how to make a UIView move up when keyboard is present
我有一个登录页面,对于较小的屏幕(例如 iPhone 4s)键盘可以覆盖登录按钮和密码文本字段等。所以我将用户名文本字段、密码文本字段和登录按钮放入 UI作为登录视图连接到 ViewController 的视图,我想检测键盘是否在登录视图上,它会推送登录视图而不是覆盖它,当键盘完全打开时推送将停止,所以 loginView 只会站在键盘上。当键盘关闭时,它会拉动 loginView 直到它到达第一个位置。
我试过了,但我推送了所有视图
var kbHeight: CGFloat!
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil);
func animateTextField(up: Bool) {
var movement = (up ? -kbHeight : kbHeight)
UIView.animateWithDuration(0.3, animations: {
self.view.frame = CGRectOffset(self.view.frame, 0, movement)
})
}
func keyboardWillShow(notification: NSNotification) {
if let userInfo = notification.userInfo {
if let keyboardSize = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
kbHeight = keyboardSize.height
self.animateTextField(true)
}
}
}
func keyboardWillHide(notification: NSNotification) {
self.animateTextField(false)
}
这是我在许多网站上发现的示例,人们与我有同样的问题正在使用它。我用 self.loginView.frame = CGRectOffset(self.view.frame, 0, movement)
更改了此 self.view.frame = CGRectOffset(self.view.frame, 0, movement)
行,因为我想推送我的 loginView 视图,其中包含文本字段和按钮,但它仍然无法正常工作并覆盖登录按钮,因此无法正确推送。如果有人可以向我解释如何做或向我展示解释它的来源,我会很高兴,我搜索但找不到,也许我错过了。
这是我的 UI
http://i62.tinypic.com/1bssy.png
谢谢指教
在您的代码中进行以下更改
替换这个
func keyboardWillShow(notification: NSNotification) {
if let userInfo = notification.userInfo {
if let keyboardSize = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
kbHeight = keyboardSize.height
self.animateTextField(true)
}
}
}
和
func keyboardWillShow(notification : NSNotification) {
var keyboardInfo : NSDictionary = notification.userInfo!
var kbSize : CGSize = ((keyboardInfo.objectForKey(UIKeyboardFrameEndUserInfoKey)) as! CGRect).size
var durationValue : NSNumber = keyboardInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber
var animationDuration : NSTimeInterval = durationValue.doubleValue
let rawAnimationCurveValue = (keyboardInfo[UIKeyboardAnimationCurveUserInfoKey] as! NSNumber).unsignedIntegerValue
let keyboardAnimationCurve = UIViewAnimationCurve(rawValue: rawAnimationCurveValue)
let options = UIViewAnimationOptions(UInt((keyboardInfo[UIKeyboardAnimationCurveUserInfoKey] as! NSNumber).integerValue << 16))
UIView.animateWithDuration(animationDuration, delay: 0, options:options , animations: { () -> Void in
self.loginView.frame = CGRectMake(self.loginView.frame.origin.x, self.loginView.frame.origin.y - kbSize.height, self.loginView.frame.size.width, self.loginView.frame.size.height)
}, completion: nil)
}
和
这个
func keyboardWillHide(notification: NSNotification) {
self.animateTextField(false)
}
和
func keyboardWillHide(notification : NSNotification) {
var keyboardInfo : NSDictionary = notification.userInfo!
var kbSize : CGSize = ((keyboardInfo.objectForKey(UIKeyboardFrameEndUserInfoKey)) as! CGRect).size
var durationValue : NSNumber = keyboardInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber
var animationDuration : NSTimeInterval = durationValue.doubleValue
let rawAnimationCurveValue = (keyboardInfo[UIKeyboardAnimationCurveUserInfoKey] as! NSNumber).unsignedIntegerValue
let keyboardAnimationCurve = UIViewAnimationCurve(rawValue: rawAnimationCurveValue)
let options = UIViewAnimationOptions(UInt((keyboardInfo[UIKeyboardAnimationCurveUserInfoKey] as! NSNumber).integerValue << 16))
UIView.animateWithDuration(animationDuration, delay: 0, options:options , animations: { () -> Void in
self.loginView.frame = CGRectMake(self.loginView.frame.origin.x, self.loginView.frame.origin.y + kbSize.height, self.loginView.frame.size.width, self.loginView.frame.size.height)
}, completion: nil)
}
效果不佳,但您的回答给了我一个意见。这有效,但当键盘完全打开时它有效,所以它没有动画:)
func animateTextField(up: Bool) {
var movement = (up ? -kbHeight : kbHeight)
UIView.animateWithDuration(0.3, animations: {
if (up == true) {
self.loginView.frame = CGRectMake(self.loginView.frame.origin.x, self.loginView.frame.origin.y - self.kbHeight, self.loginView.frame.size.width, self.loginView.frame.size.height)
} else {
self.loginView.frame = CGRectMake(self.loginView.frame.origin.x, self.loginView.frame.origin.y + self.kbHeight, self.loginView.frame.size.width, self.loginView.frame.size.height)
}
})
}
func keyboardWillHide(notification: NSNotification) {
self.animateTextField(false)
}
func keyboardDidShow(notification: NSNotification) {
if let userInfo = notification.userInfo {
if let keyboardSize = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
kbHeight = keyboardSize.height - self.loginView.frame.size.height
self.animateTextField(true)
}
}
}
当我在 userInfo 行上放置一个断点时,我得到了这个
[UIKeyboardFrameBeginUserInfoKey: NSRect: {{0, 480}, {320, 216}}, UIKeyboardCenterBeginUserInfoKey: NSPoint: {160, 588}, UIKeyboardFrameEndUserInfoKey: NSRect: {{0, 264}, {320, 216}}, UIKeyboardCenterEndUserInfoKey: NSPoint: {160, 372}, UIKeyboardAnimationDurationUserInfoKey: 0.25, UIKeyboardBoundsUserInfoKey: NSRect: {{0, 0}, {320, 216}}, UIKeyboardAnimationCurveUserInfoKey: 7]
如何在 keyboardWillShow 函数中启动动画并使用键盘打开动画。我的意思是键盘会开始显示,当它到达视图的边缘时,它会开始推动。我需要在 keyboardWillShow 函数中执行此操作,因为 keyboardDidShow 可以工作但不能同时推送视图
我有一个登录页面,对于较小的屏幕(例如 iPhone 4s)键盘可以覆盖登录按钮和密码文本字段等。所以我将用户名文本字段、密码文本字段和登录按钮放入 UI作为登录视图连接到 ViewController 的视图,我想检测键盘是否在登录视图上,它会推送登录视图而不是覆盖它,当键盘完全打开时推送将停止,所以 loginView 只会站在键盘上。当键盘关闭时,它会拉动 loginView 直到它到达第一个位置。
我试过了,但我推送了所有视图
var kbHeight: CGFloat!
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil);
func animateTextField(up: Bool) {
var movement = (up ? -kbHeight : kbHeight)
UIView.animateWithDuration(0.3, animations: {
self.view.frame = CGRectOffset(self.view.frame, 0, movement)
})
}
func keyboardWillShow(notification: NSNotification) {
if let userInfo = notification.userInfo {
if let keyboardSize = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
kbHeight = keyboardSize.height
self.animateTextField(true)
}
}
}
func keyboardWillHide(notification: NSNotification) {
self.animateTextField(false)
}
这是我在许多网站上发现的示例,人们与我有同样的问题正在使用它。我用 self.loginView.frame = CGRectOffset(self.view.frame, 0, movement)
更改了此 self.view.frame = CGRectOffset(self.view.frame, 0, movement)
行,因为我想推送我的 loginView 视图,其中包含文本字段和按钮,但它仍然无法正常工作并覆盖登录按钮,因此无法正确推送。如果有人可以向我解释如何做或向我展示解释它的来源,我会很高兴,我搜索但找不到,也许我错过了。
这是我的 UI http://i62.tinypic.com/1bssy.png
谢谢指教
在您的代码中进行以下更改
替换这个
func keyboardWillShow(notification: NSNotification) {
if let userInfo = notification.userInfo {
if let keyboardSize = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
kbHeight = keyboardSize.height
self.animateTextField(true)
}
}
}
和
func keyboardWillShow(notification : NSNotification) {
var keyboardInfo : NSDictionary = notification.userInfo!
var kbSize : CGSize = ((keyboardInfo.objectForKey(UIKeyboardFrameEndUserInfoKey)) as! CGRect).size
var durationValue : NSNumber = keyboardInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber
var animationDuration : NSTimeInterval = durationValue.doubleValue
let rawAnimationCurveValue = (keyboardInfo[UIKeyboardAnimationCurveUserInfoKey] as! NSNumber).unsignedIntegerValue
let keyboardAnimationCurve = UIViewAnimationCurve(rawValue: rawAnimationCurveValue)
let options = UIViewAnimationOptions(UInt((keyboardInfo[UIKeyboardAnimationCurveUserInfoKey] as! NSNumber).integerValue << 16))
UIView.animateWithDuration(animationDuration, delay: 0, options:options , animations: { () -> Void in
self.loginView.frame = CGRectMake(self.loginView.frame.origin.x, self.loginView.frame.origin.y - kbSize.height, self.loginView.frame.size.width, self.loginView.frame.size.height)
}, completion: nil)
}
和
这个
func keyboardWillHide(notification: NSNotification) {
self.animateTextField(false)
}
和
func keyboardWillHide(notification : NSNotification) {
var keyboardInfo : NSDictionary = notification.userInfo!
var kbSize : CGSize = ((keyboardInfo.objectForKey(UIKeyboardFrameEndUserInfoKey)) as! CGRect).size
var durationValue : NSNumber = keyboardInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber
var animationDuration : NSTimeInterval = durationValue.doubleValue
let rawAnimationCurveValue = (keyboardInfo[UIKeyboardAnimationCurveUserInfoKey] as! NSNumber).unsignedIntegerValue
let keyboardAnimationCurve = UIViewAnimationCurve(rawValue: rawAnimationCurveValue)
let options = UIViewAnimationOptions(UInt((keyboardInfo[UIKeyboardAnimationCurveUserInfoKey] as! NSNumber).integerValue << 16))
UIView.animateWithDuration(animationDuration, delay: 0, options:options , animations: { () -> Void in
self.loginView.frame = CGRectMake(self.loginView.frame.origin.x, self.loginView.frame.origin.y + kbSize.height, self.loginView.frame.size.width, self.loginView.frame.size.height)
}, completion: nil)
}
效果不佳,但您的回答给了我一个意见。这有效,但当键盘完全打开时它有效,所以它没有动画:)
func animateTextField(up: Bool) {
var movement = (up ? -kbHeight : kbHeight)
UIView.animateWithDuration(0.3, animations: {
if (up == true) {
self.loginView.frame = CGRectMake(self.loginView.frame.origin.x, self.loginView.frame.origin.y - self.kbHeight, self.loginView.frame.size.width, self.loginView.frame.size.height)
} else {
self.loginView.frame = CGRectMake(self.loginView.frame.origin.x, self.loginView.frame.origin.y + self.kbHeight, self.loginView.frame.size.width, self.loginView.frame.size.height)
}
})
}
func keyboardWillHide(notification: NSNotification) {
self.animateTextField(false)
}
func keyboardDidShow(notification: NSNotification) {
if let userInfo = notification.userInfo {
if let keyboardSize = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
kbHeight = keyboardSize.height - self.loginView.frame.size.height
self.animateTextField(true)
}
}
}
当我在 userInfo 行上放置一个断点时,我得到了这个
[UIKeyboardFrameBeginUserInfoKey: NSRect: {{0, 480}, {320, 216}}, UIKeyboardCenterBeginUserInfoKey: NSPoint: {160, 588}, UIKeyboardFrameEndUserInfoKey: NSRect: {{0, 264}, {320, 216}}, UIKeyboardCenterEndUserInfoKey: NSPoint: {160, 372}, UIKeyboardAnimationDurationUserInfoKey: 0.25, UIKeyboardBoundsUserInfoKey: NSRect: {{0, 0}, {320, 216}}, UIKeyboardAnimationCurveUserInfoKey: 7]
如何在 keyboardWillShow 函数中启动动画并使用键盘打开动画。我的意思是键盘会开始显示,当它到达视图的边缘时,它会开始推动。我需要在 keyboardWillShow 函数中执行此操作,因为 keyboardDidShow 可以工作但不能同时推送视图