首页 > 代码库 > 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自动上升
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。