美文网首页
通知方法,获取键盘的左上角的point值

通知方法,获取键盘的左上角的point值

作者: 丝丝雨凉 | 来源:发表于2018-04-25 11:47 被阅读7次

    通知方法,获取键盘的左上角的point值,方便之后做其他的操作。

    NotificationCenter.default.addObserver(self, selector: #selector(keyBoardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)

    NotificationCenter.default.addObserver(self, selector: #selector(keyBoardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)

    //  当键盘弹出时调用的方法

    func keyBoardWillShow(_ notification:Notification){

    let dict = NSDictionary(dictionary: notification.userInfo!)

    let keyBoardFrame = (dict[UIKeyboardFrameEndUserInfoKey]! asAnyObject).cgRectValue

    let point = keyBoardFrame?.origin

    self.frame = CGRect(x: (point?.x)!, y: (point?.y)! - mViewHeight, width: mWidth, height: mViewHeight)

    }

    func keyBoardWillHide(_ notification: Notification) {

    self.frame = CGRect(x: 0, y: mHeight - mViewHeight, width: mWidth, height: mViewHeight)

    }

    相关文章

      网友评论

          本文标题:通知方法,获取键盘的左上角的point值

          本文链接:https://www.haomeiwen.com/subject/hfsmpttx.html