iOS键盘下的空白区域

时间:2019-05-12 21:45:08

标签: ios ipad uitextview

在我的iOS应用中,第二次出现键盘后,键盘下方有一个空格。

Keyboard space

.?123后,空格将隐藏。

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

我的iPhone 7不会发生这种情况,但我的iPad第六代会发生这种情况。这很奇怪,因为它只是在此特定“文本视图”中的我的应用程序上。

这发生在某人身上吗?我在Google中找不到任何内容。

以下是视图控制器的源代码:https://github.com/ColdGrub1384/Pyto/blob/master/Pyto/View%20Controllers/EditorViewController.swift

我使用一个内部包含UITextView的视图进行语法高亮显示,但这与bug无关,因为我也使用标准UITextView进行了测试。

2 个答案:

答案 0 :(得分:0)

@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

答案 1 :(得分:0)

iOS / iPadOS 13修复了该错误。