gpt4 book ai didi

ios - 如何在键盘存在时使 UIView 向上 move

转载 作者:搜寻专家 更新时间:2023-11-01 06:44:42 26 4
gpt4 key购买 nike

我有一个登录页面,对于较小的屏幕(例如 iPhone 4s),键盘可以覆盖在登录按钮和密码文本字段等上。所以我将用户名文本字段、密码文本字段和登录按钮放入连接到 ViewController 的 UIView 中作为 loginView,我想检测键盘是否在 loginView 上,它会推送 loginView 而不是覆盖在其上,并且当键盘完全打开时推送将停止,因此 loginView 将仅位于键盘上。当键盘关闭时,它会拉动 loginView 直到它到达第一个位置。

我试过了,但我“推送所有 View ”

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.view.frame = CGRectOffset(self.view.frame, 0, movement)
符合 self.loginView.frame = CGRectOffset(self.view.frame, 0, movement)
因为我想推送我的 loginView View ,其中包含文本字段和按钮,但它仍然无法正常工作并覆盖登录按钮,因此无法正确推送。如果有人可以向我解释如何做或向我展示解释它的来源,我会很高兴,我搜索但找不到,也许我错过了。

这是我的用户界面 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)
}

关于ios - 如何在键盘存在时使 UIView 向上 move ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31646912/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com