【问题标题】:UIKeyboardWillShowNotification call does not animate UIToolbarUIKeyboardWillShowNotification 调用不会为 UIToolbar 设置动画
【发布时间】:2015-03-05 00:14:08
【问题描述】:

我有一个UIToolbar,其文本字段在显示键盘时应该向上移动。

在我的viewWillAppear 中,我注册了我的观察者:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)

(viewWillDisappear 删除我的观察者)

以及功能:

func keyboardWillShow(notification: NSNotification) {
    println("will show")

    let userInfo = notification.userInfo!

    let keyboardFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as NSValue).CGRectValue()
    let animationDuration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as NSNumber).doubleValue

    let animations: () -> () = {
        self.toolBar.frame.origin.y = self.view.frame.size.height - keyboardFrame.height - self.toolBar.frame.height
    }

    if animationDuration > 0 {
        let options = UIViewAnimationOptions(UInt((userInfo[UIKeyboardAnimationCurveUserInfoKey] as NSNumber).integerValue << 16)) // http://stackoverflow.com/a/18873820/242933
        UIView.animateWithDuration(animationDuration, delay: 0, options: options, animations: animations, completion: nil)
    } else {
        animations()
    }
}

现在当我点击UITextField 时,键盘会出现并调用keyboardWillShow。但是动画直到 keyboardWillShow 被第二次调用时才会发生(除非高度发生变化,否则具有多个键盘不起作用;例如,当自动完成栏改变状态时)。

我做错了什么?我所看到的任何地方都是以这种方式实现的。

编辑:更多细节:

animations() 以工具栏的正确高度调用,但 UI 没有将工具栏放在正确的位置。也许它还没有初始化?

更多细节:当我将UIToolbar 用作inputAccessoryView 时,当我点击UITextField 时,它不会出现在键盘上方,但当我开始打字时,它会出现。我这里的选项用完了...

更多尝试找出问题:似乎运行任何其他动画都有效,例如更改工具栏的高度。

【问题讨论】:

    标签: ios uitoolbar uikeyboard uianimation


    【解决方案1】:

    我在this GitHub repository 找到了适合我的答案

        let animations: () -> () = {
            if !(self.tableView.tracking || self.tableView.decelerating) {
                // Move content with keyboard
                if overflow > 0 {                   // scrollable before
                    self.tableView.contentOffset.y += insetChange
                    if self.tableView.contentOffset.y < -insetOld.top {
                        self.tableView.contentOffset.y = -insetOld.top
                    }
                } else if insetChange > -overflow { // scrollable after
                    self.tableView.contentOffset.y += insetChange + overflow
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2012-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-26
      • 2019-12-24
      • 1970-01-01
      相关资源
      最近更新 更多