提问者:小点点

当键盘覆盖它时如何移动UITextView


当需要一些输入时,我需要将一个< code>UITextView从键盘上移开。我正在使用下面的代码,它在< code>UITextField上运行得很好,但在< code>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)
}

请建议我缺少什么?


共3个答案

匿名用户

实际的解决方案非常简单。

在UITextView上,将scrollingEnabled设置为False。

感谢所有的帮助。

匿名用户

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)
    }
}

匿名用户

首先你设置这个分数

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

调用text view委托后

 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)

它在我这边工作得很好,请检查一下。这可能对你很有帮助。