step 1:
//监听键盘改变 NSNotificationCenter.defaultCenter().addobserver(self,selector:#selector(CommentDetailViewController.keyboardWillChange(_:)),name:UIKeyboardWillChangeFrameNotification,object: nil)
step 2:
实现监听方法
func keyboardWillChange(note:NSNotification){ let duration:Double = (note.userInfo![UIKeyboardAnimationDurationUserInfoKey]?.doubleValue)! let keyboardY:CGFloat = (note.userInfo![UIKeyboardFrameEndUserInfoKey]?.CGRectValue().origin.y)! let ty = keyboardY - SCREEN_HEIGHT UIView.animateWithDuration(duration) { self.fieldBar?.transform = CGAffineTransformMakeTranslation(0,ty) } } // 移除监听 deinit{ NSNotificationCenter.defaultCenter().removeObserver(self) }
另:限制输入框字数
实现 UITextFieldDelegate 方法
func textFieldDidChange(textField:UITextField){ let cleanString = textField.text!.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) if(Nsstring(string: textField.text!).length > 150){ textField.text = Nsstring(string: cleanString).substringToIndex(150) Util.showMessage("150字够多了哦") } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。