gpt4 book ai didi

ios - iOS 键盘下的空白区域

转载 作者:可可西里 更新时间:2023-11-01 03:34:47 25 4
gpt4 key购买 nike

在我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com