首页 > 代码库 > swift 弹出键盘view自动上升

swift 弹出键盘view自动上升

一、设置监听键盘事件函数:

override func viewWillAppear(animated: Bool) {        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, 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)            //let keyboardheight  = keyboardSize.height as CGFloat            //let frame = self.nameInput.frame            // frame.origin.y = frame.origin.y - keyboardheight            //var offset = 156 as CGFloat            var width = self.view.frame.size.width;            var height = self.view.frame.size.height;            let rect = CGRectMake(0.0, -156,width,height);            self.view.frame = rect        }    }

三、处理收回事件:

func keyboardWillHide(notification:NSNotification){        self.view.addSubview(logoArea)        var width = self.view.frame.size.width;        var height = self.view.frame.size.height;        let rect = CGRectMake(0.0, 0,width,height);        self.view.frame = rect    }

 

swift 弹出键盘view自动上升