swift - 键盘显示不正确

标签 swift keyboard

我有以下功能使键盘不覆盖 TextView,但键盘显示不正确。相反,出现了一种没有键盘按键的全黑“键盘”。

func textViewDidBeginEditing(_ textView: UITextView) {
    moveTextView(textView, moveDistance: -250, up: true)
}

func textViewDidEndEditing(_ textView: UITextView) {
    moveTextView(textView, moveDistance: -250, up: false)
}

func textViewShouldReturn(_ textView: UITextView) -> Bool {
    textView.resignFirstResponder()
    return true
}

func moveTextView(_ textView: UITextView, moveDistance: Int, up: Bool) {
    let moveDuration = 0.3
    let movement: CGFloat = CGFloat(up ? moveDistance : -moveDistance)

    UIView.beginAnimations("animateTextView", context: nil)
    UIView.setAnimationBeginsFromCurrentState(true)
    UIView.setAnimationDuration(moveDuration)
    self.view.frame = self.view.frame.offsetBy(dx: 0, dy: movement)
    UIView.commitAnimations()
}

有人知道为什么以及如何修复它吗?

感谢您的宝贵时间。

最佳答案

尝试换一种方式。将 UITextViewDelegate 添加到您的 viewController。在 viewDidLoad() 中添加这样的东西:

self.yourTextView1.delegate = self
self.yourTextView2.delegate = self

//For scrolling the view if keyboard on
NotificationCenter.default.addObserver(self, selector: #selector(YourViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(YourViewController.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)

并将其添加到您的 ViewController 中:

var keyBoardAlreadyShowed = false //using this to not let app to scroll view
//if we tapped UITextField and then another UITextField
func keyboardWillShow(notification: NSNotification) {
    if !keyBoardAlreadyShowed {
        self.view.frame.origin.y -= 50 // we will scroll on it
        keyBoardAlreadyShowed = true
    }
}

func keyboardWillHide(notification: NSNotification) {
    self.view.frame.origin.y += 50
    keyBoardAlreadyShowed = false
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    self.view.endEditing(true)
}

希望对你有帮助

关于swift - 键盘显示不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42996044/

相关文章:

c# - WPF MouseLeftButtonDown 事件处理程序中的 Ctrl 键按下条件

ios - 如何在我的 UICollectionView 上重新加载数据?

ios - 模态表可以在 SwiftUI 中有导航栏吗?

ios - 在 xcode 12 中创建通用框架时出现 Lipo 错误

swift - 如何在 Alamofire 中使用表单数据

ios - Swift - 多个 UI 更改相互阻塞

java - 使用 Java HID API(管理来自多个键盘的输入)打开 HID 设备时出现 NullPointerException

ios - 安装应用程序-推送通知alertview回调

swift - RealmSwift 错误 : "RLMException, reason: Collection was mutated while being enumerated."

javascript - HTML 可点击鼓组,无法在 CSS/HTML 中设置背景颜色/如何将键盘键分配给 HTML 按钮?