【问题标题】:Keyboard height needed before the textfield delegate is called调用文本字段委托之前所需的键盘高度
【发布时间】:2016-06-21 01:24:46
【问题描述】:

所以我有以下代码:

 //MARK: - Textfield delegate Methods

    func textFieldDidBeginEditing(textField: UITextField)
    {

        // update view based on updates in the constraints
        self.view.layoutIfNeeded()
        //perform animation to grow the dockview
        UIView.animateWithDuration(0.5, animations: { () -> Void in
            self.dockViewHeightConstraint.constant = self.keyBoardHeight + 60
            self.view.layoutIfNeeded()
            }, completion: nil)
    }

override func viewDidLoad() {
    super.viewDidLoad()
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)        
}


func keyboardWillShow(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
        let contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0)
        keyBoardHeight =  contentInsets.bottom
    }
}

我想要做的是以下。当用户单击文本字段时,键盘应该打开,并且我制作的视图应该向上移动。 我通过 NSNotification 触发的keyboardWillShow 函数从键盘获取高度。

问题如下:当我运行代码时,textFieldDidBeginEditingNSNotification 之前被调用,使我的 self.keyBoardHeight 值为 0(初始值)。有没有办法在按下 textField 之前调用 NSNotification 部分?

【问题讨论】:

  • 你必须使用 TPKeyboardAvoiding 第三方库
  • 只给出一个更好的初始猜测,而不是 0。
  • @Birendra 谢谢,但应该有不同的方法来解决这个问题,而不是在整个第三方库中复制

标签: ios swift keyboard uitextfield nsnotificationcenter


【解决方案1】:

在 viewController 中设置 isKeyboardShown 标志来跟踪是否显示键盘,与元素“myElement”相交的高度可以在 fieldMovement 属性中捕获,如下所示

    if !isKeyboardShown
            {
                if let info = notification.userInfo
                {
                    if fieldMovement == nil
                    {
                        let window = UIApplication.sharedApplication().keyWindow
                        var keyboardEndRect = CGRectZero

                        //Get frame of keyboard view object
                        ((info[UIKeyboardFrameEndUserInfoKey] as! NSValue)).getValue(&keyboardEndRect)

                        /* Convert the frame from the window's coordinate system to our view's coordinate system */
                        keyboardEndRect = view.convertRect(keyboardEndRect, fromView: window)

                        /* Find out how much of our view is being covered by the keyboard */
                        fieldMovement = CGRectIntersection(myElement.frame, keyboardEndRect).height
                    }

                    //Get animation duration of keyboard
                    var animationDuration: NSTimeInterval = 0
                    (info[UIKeyboardAnimationDurationUserInfoKey] as! NSValue).getValue(&animationDuration)

                    // Move views up by intersected height
                UIView.animateWithDuration(animationDuration)
                {
                    //animate your objects here
                }
            }
            isKeyboardShown = true
        }

【讨论】:

  • 这如何让我的 NSNotification 在 textFieldDidBeginEditing 之前关闭?
  • 您不需要实现 textFieldDidBeginEditing: 方法,keyboardWillShow 和 keyboardWillHide 通知是从键盘而不是 textField 接收的
猜你喜欢
  • 2014-04-18
  • 2021-09-02
  • 2017-03-15
  • 2016-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多