enter image description here当需要输入时,我需要将UITextView
移开键盘。我使用以下代码,它与UITextField
完美配合,但与UITextView
完全无关。
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self,
selector: #selector(DailyNotesViewController.keyboardWillShow(_:)),
name: UIKeyboardWillShowNotification,
object: nil)
NSNotificationCenter.defaultCenter().addObserver(self,
selector: #selector(DailyNotesViewController.keyboardWillHide(_:)),
name: UIKeyboardWillHideNotification,
object: nil)
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
func adjustInsetForKeyboardShow(show: Bool, notification: NSNotification) {
let userInfo = notification.userInfo ?? [:]
let keyboardFrame = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()
let adjustmentHeight = (CGRectGetHeight(keyboardFrame) + 20) * (show ? 1 : -1)
scrollView.contentInset.bottom += adjustmentHeight
scrollView.scrollIndicatorInsets.bottom += adjustmentHeight
}
func keyboardWillShow(notification: NSNotification) {
adjustInsetForKeyboardShow(true, notification: notification)
}
func keyboardWillHide(notification: NSNotification) {
adjustInsetForKeyboardShow(false, notification: notification)
}
请建议我缺少什么?
答案 0 :(得分:3)
实际的解决方案非常简单。
在UITextView上,将scrollingEnabled设置为False。
感谢所有的帮助。
答案 1 :(得分:0)
func animateTextView(textView: UITextView, up: Bool)
{
let movementDistance:CGFloat = -170
let movementDuration: Double = 0.3
var movement:CGFloat = 0
if up
{
movement = movementDistance
}
else
{
movement = -movementDistance
}
UIView.beginAnimations("animateTextView", context: nil)
UIView.setAnimationBeginsFromCurrentState(true)
UIView.setAnimationDuration(movementDuration)
self.view.frame = self.view.frame.offsetBy(dx: 0, dy: movement)
UIView.commitAnimations()
}
func textViewDidBeginEditing(_ textView: UITextView)
{
if (textView.tag == 1) { // remarks textview
self.animateTextView(textView: textView, up:true)
}
}
func textViewDidEndEditing(_ textView: UITextView)
{
if (textView.tag == 1) { // remarks textview
self.animateTextView(textView: textView, up:false)
}
}
答案 2 :(得分:-1)
首先设置此分数
var animatedDistance: CGFloat = 0.0
let KEYBOARD_ANIMATION_DURATION: CGFloat = 0.3
let MINIMUM_SCROLL_FRACTION: CGFloat = 0.2
let MAXIMUM_SCROLL_FRACTION: CGFloat = 0.8
let PORTRAIT_KEYBOARD_HEIGHT: CGFloat = 120
let LANDSCAPE_KEYBOARD_HEIGHT: CGFloat = 140
调用textviewdelegate后
func textViewDidBeginEditing(textView: UITextView) {
var textFieldRect = self.view.window.convertRect(textView.bounds, fromView: textView)
var viewRect = self.view.window.convertRect(self.view.bounds, fromView: self.view)
var midline: CGFloat = textFieldRect.origin.y + 0.5 * textFieldRect.size.height
var numerator: CGFloat = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height
var denominator: CGFloat = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height
var heightFraction: CGFloat = numerator / denominator
if heightFraction < 0.0 {
heightFraction = 0.0
}
else if heightFraction > 1.0 {
heightFraction = 1.0
}
var orientation = UIApplication.sharedApplication().statusBarOrientation
if orientation == .Portrait || orientation == .PortraitUpsideDown {
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction)
我的工作正常,请检查。它可能对你有帮助。