【问题标题】:how to make a UIView move up when keyboard is present键盘存在时如何使 UIView 向上移动
【发布时间】:2015-10-17 06:34:19
【问题描述】:

我有一个登录页面,对于较小的屏幕(例如 iPhone 4s),键盘可以覆盖登录按钮和密码文本字段等。所以我将用户名文本字段、密码文本字段和登录按钮放入连接到 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.loginView.frame = CGRectOffset(self.view.frame, 0, movement) 更改了这个self.view.frame = CGRectOffset(self.view.frame, 0, movement) 行,因为我想推送我的loginView 视图,它里面有文本字段和按钮,但它仍然不起作用并且覆盖登录按钮,所以不能正确推送。如果有人能解释我如何做或向我展示解释它的来源,我会很高兴,我搜索但找不到,也许我错过了。

这是我的用户界面 http://i62.tinypic.com/1bssy.png

感谢您的建议

【问题讨论】:

  • 你可以使用 TPkeyboard 避免滚动视图。

标签: ios swift uiview keyboard move


【解决方案1】:

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

替换这个

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

【讨论】:

  • 首先非常感谢您的回答,它对我帮助很大。我回答了你的帖子,但我太长了,所以我把它作为一个新答案发布,所以请检查我的帖子:)
  • 我检查了你的帖子。如果您需要在键盘中设置动画会出现方法,请参阅我的更新答案。
  • 它没有用。我们需要同时更新 kbSize,而不是将其设置为 CGSize = ((keyboardInfo.objectForKey(UIKeyboardFrameEndUserInfoKey)) as!CGRect).size (我这样更改这一行是因为 "CGSize = ((keyboardInfo.objectForKey(UIKeyboardFrameEndUserInfoKey)) as! CGRect).size" 给出错误)因为它给它一个固定值,但它在 0.25 秒内从 0 变为 216,所以当它发生变化时,我认为我们也需要更改 kbSize。你怎么看?
  • "我们需要同时更新 kbSize" 我没听懂?为什么我们需要更新。当键盘准备好出现时,keyboardWillShow 通知只会触发一次。上面代码中是否有任何运行时错误,请告诉我,因为我没有测试上面的代码。
  • 我使用 TPkeyboardavoidingscrollview 而不是试图解决这个问题 ;) 感谢您的支持,至少我理解逻辑,即使我无法解决问题 :)
【解决方案2】:

效果不佳,但您的回答给了我一个意见。这行得通,但是当键盘完全打开时它可以工作,所以它没有动画:)

    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 有效但不能同时推送视图

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-10
    • 1970-01-01
    • 1970-01-01
    • 2018-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-15
    相关资源
    最近更新 更多