如何在键盘存在时使UIView向上移动

时间:2015-07-27 07:09:13

标签: ios swift uiview keyboard move

我有一个登录页面和较小的屏幕(例如iPhone 4s)键盘可以覆盖登录按钮和密码文本字段等。所以我将用户名textfield,密码文本字段和登录按钮放入连接到ViewController的UIView作为loginView,我想检测键盘是否在loginView上,它会推送loginView而不是覆盖在它上面,并且当键盘完全打开时推送将停止,因此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.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

感谢您的建议

2 个答案:

答案 0 :(得分:3)

在您的代码中进行以下更改

替换此

    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)
    }

答案 1 :(得分:0)

它没有奏效,但你的回答给了我一个意见。这是有效的,但它在键盘完全打开时有效,因此它不能动画:)

    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可以工作但不能同时推送视图