ios - iOS 键盘下的空白区域

标签 ios ipad uitextview

在我的 iOS 应用中,第二次显示键盘后,键盘下方有一个空白区域。

Keyboard space

.?123后空格隐藏。

我删除了 keyboardWillShowNotificationkeyboardWillHideNotification 并且发生了同样的事情。我在 viewWillTransitionToSize:withTransitionCoordinator: 处调整了 UITextView 的大小,但我删除了调整大小的代码,但同样的事情发生了。

我的 iPhone 7 不会发生这种情况,但我的 iPad 6th gen 会发生这种情况。这很奇怪,因为它就在我的应用程序的这个特定 TextView 中。

有人遇到过这种情况吗?我在 Google 中找不到任何内容。

这里是 View Controller 的源代码:https://github.com/ColdGrub1384/Pyto/blob/master/Pyto/View%20Controllers/EditorViewController.swift

我使用其中包含 UITextView 的 View 来突出显示语法,但这与错误无关,因为我还使用标准 UITextView 进行了测试。

最佳答案

@objc protocol KeyboardConstraintListener: class
{
func didOpenKeyboard()
func didCloseKeyboard()
}

class KeyboardConstraint: NSLayoutConstraint
{
@IBOutlet
weak var listener: KeyboardConstraintListener?

@IBInspectable
var skipAutoCalculateMarginToBottom: Bool = false

@IBInspectable
var keepMarginWhenOpen: Bool = false

private var originalConstant: CGFloat = 0.0

override func awakeFromNib()
{
    super.awakeFromNib()
    self.originalConstant = self.constant

    NotificationCenter.default.addObserver(self,
                                           selector: #selector(keyboardWillShow),
                                           name: UIResponder.keyboardWillShowNotification,
                                           object: nil)

    NotificationCenter.default.addObserver(self,
                                           selector: #selector(keyboardWillHide),
                                           name: UIResponder.keyboardWillHideNotification,
                                           object: nil)
}

deinit
{
    NotificationCenter.default.removeObserver(self)
}

@objc func keyboardWillShow(notification: Notification)
{
    self.listener?.didOpenKeyboard()
    self.updateConstant(notification: notification, showing: true)
}

@objc func keyboardWillHide(notification: Notification)
{
    self.listener?.didCloseKeyboard()
    self.updateConstant(notification: notification, showing: false)
}

private func updateConstant(notification: Notification, showing: Bool)
{
    let duration: TimeInterval = (notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double) ?? 0.2

    guard let endFrame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect else
    {
        return
    }

    guard let firstView: UIView = self.getView(item: self.firstItem) else
    {
        return
    }

    guard let secondView: UIView = self.getView(item: self.secondItem) else
    {
        return
    }

    guard let superview: UIView = firstView.superview else
    {
        return
    }

    let keyboardHeight: CGFloat = (showing ? max(endFrame.size.height - self.modifier(view: secondView), 0.0) : 0.0)

    CATransaction.begin()
    CATransaction.setDisableActions(true)
    superview.layoutIfNeeded()
    CATransaction.commit()

    UIView.animate(
        withDuration: duration,
        delay: 0,
        options: [.curveLinear],
        animations:
        {
            self.constant = keyboardHeight + self.originalConstant
            superview.layoutIfNeeded()
    },
        completion: nil)
}

private func modifier(view: UIView) -> CGFloat
{
    guard let window: UIWindow = view.window else
    {
        return 0.0
    }

    guard let superview: UIView = view.superview else
    {
        return 0.0
    }

    guard !self.skipAutoCalculateMarginToBottom else
    {
        return 0.0
    }

    let origin: CGPoint = superview.convert(view.frame.origin, to: nil)
    let bottom = origin.y + view.bounds.size.height
    let expand = window.bounds.size.height - bottom
    return expand - (self.constant - self.originalConstant) - (self.keepMarginWhenOpen ? self.originalConstant : 0.0)
}

private func getView(item: AnyObject?) -> UIView?
{
    if let view = item as? UIView
    {
        return view
    }
    else if let view = (item as? UILayoutGuide)?.owningView
    {
        return view
    }
    return nil
}

创建一个约束使得 textView.bottom = safeArea.bottom

关于ios - iOS 键盘下的空白区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56103723/

相关文章:

ios - 无法转换类型 'NSString' 的值?预期参数类型 'String'

ios - 在动画过程中改变位置?

ios - React Native iPad 分屏组件

swift - 当我更改字距调整时,UITextView 样式会重置

ios - 如何防止 UITextView 文本在打字时移动到顶部?

ios - Xcode - Swift,UITextView 不会显示在 UIStackView 上

ios - 在没有 Storyboard 的情况下在 UIView 中添加 UICollectionView

ios - 果酱 sdk UnicodeDecodeError

iphone - UIView 内的 UIButton 不可点击

iphone - "Code Sign error: No unexpired provisioning profiles found that contain any of the keychain' s 签名证书"