在键盘上方移动文本字段

时间:2015-12-11 10:16:00

标签: ios swift keyboard scrollview

我一直在实现文档中给出的代码,用于推动UIViews,如键盘上方的文本字段,使其可见 - 我理解并成功实现了代码,但我只是对特定函数中的某些行感到困惑。有人可以解释一下吗?我评论过关于编码的观点。如果我错了,请纠正我?

func keyboardWasShown(notification: NSNotification)
    {
        //Need to calculate keyboard exact size due to Apple suggestions
        self.scrollView.scrollEnabled = true//enable the scrol view
        let info : NSDictionary = notification.userInfo!//get the userinfo for height of the keyboard
        let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue().size//get the keyboard height
        let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize!.height, 0.0)

        self.scrollView.contentInset = contentInsets//adjusts the bottom content inset of the scroll view by the height of the keyboard , contentInset - The distance that the content view is inset from the enclosing scroll view.

        self.scrollView.scrollIndicatorInsets = contentInsets//scrollIndicatorInsets - The distance the scroll indicators are inset from the edge of the scroll view.

        var aRect : CGRect = self.view.frame
        aRect.size.height -= keyboardSize!.height
        if let activeFieldPresent = activeField//check for the textfield that is active
        {
            if (!CGRectContainsPoint(aRect, activeField!.frame.origin))
            {
                self.scrollView.scrollRectToVisible(activeField!.frame, animated: true)
            }
        }


    }

现在我的问题是这段代码中发生了什么 -

var aRect : CGRect = self.view.frame
            aRect.size.height -= keyboardSize!.height
            if let activeFieldPresent = activeField//check for the textfield that is active
            {
                if (!CGRectContainsPoint(aRect, activeField!.frame.origin))
                {
                    self.scrollView.scrollRectToVisible(activeField!.frame, animated: true)
                }
            }

请解释一下。谢谢,我的回答非常有帮助。

0 个答案:

没有答案