iOS 键盘下的空 space
Empty space under iOS keyboard
在我的 iOS 应用程序中,第二次显示键盘后,键盘下方有一个空白 space。
按.?123
后space隐藏。
我删除了 keyboardWillShowNotification
和 keyboardWillHideNotification
,同样的事情发生了。我在 viewWillTransitionToSize:withTransitionCoordinator:
处调整了 UITextView
的大小,但我删除了调整大小的代码,同样的事情发生了。
我的 iPhone 7 不会发生这种情况,但我的 iPad 6 代会发生这种情况。这很奇怪,因为它就在我的应用程序的这个特定文本视图中。
有人遇到过这种情况吗?我在 Google.
中找不到任何内容
这是视图控制器的源代码:https://github.com/ColdGrub1384/Pyto/blob/master/Pyto/View%20Controllers/EditorViewController.swift
我使用其中包含 UITextView
的视图来突出显示语法,但这与错误无关,因为我还使用标准 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 / iPadOS 13 修复了这个错误。
在我的 iOS 应用程序中,第二次显示键盘后,键盘下方有一个空白 space。
按.?123
后space隐藏。
我删除了 keyboardWillShowNotification
和 keyboardWillHideNotification
,同样的事情发生了。我在 viewWillTransitionToSize:withTransitionCoordinator:
处调整了 UITextView
的大小,但我删除了调整大小的代码,同样的事情发生了。
我的 iPhone 7 不会发生这种情况,但我的 iPad 6 代会发生这种情况。这很奇怪,因为它就在我的应用程序的这个特定文本视图中。
有人遇到过这种情况吗?我在 Google.
中找不到任何内容这是视图控制器的源代码:https://github.com/ColdGrub1384/Pyto/blob/master/Pyto/View%20Controllers/EditorViewController.swift
我使用其中包含 UITextView
的视图来突出显示语法,但这与错误无关,因为我还使用标准 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 / iPadOS 13 修复了这个错误。