Fix safe area on Compose screen not including keyboard on iOS 13

This commit is contained in:
Shadowfacts 2020-09-07 17:05:50 -04:00
parent 9a4ddfea3f
commit b4ddb8f533
Signed by untrusted user: shadowfacts
GPG Key ID: 94A5AB95422746E5
1 changed files with 25 additions and 12 deletions

View File

@ -21,6 +21,9 @@ class ComposeHostingController: UIHostingController<ComposeContainerView> {
private var cancellables = [AnyCancellable]() private var cancellables = [AnyCancellable]()
private var keyboardHeight: CGFloat = 0
private var toolbarHeight: CGFloat = 44
private var mainToolbar: UIToolbar! private var mainToolbar: UIToolbar!
private var inputAccessoryToolbar: UIToolbar! private var inputAccessoryToolbar: UIToolbar!
private var visibilityBarButtonItems = [UIBarButtonItem]() private var visibilityBarButtonItems = [UIBarButtonItem]()
@ -54,7 +57,7 @@ class ComposeHostingController: UIHostingController<ComposeContainerView> {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidHide(_:)), name: UIResponder.keyboardDidHideNotification, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidHide(_:)), name: UIResponder.keyboardDidHideNotification, object: nil)
// add the height of the toolbar itself to the bottom of the safe area so content inside SwiftUI ScrollView doesn't underflow it // add the height of the toolbar itself to the bottom of the safe area so content inside SwiftUI ScrollView doesn't underflow it
additionalSafeAreaInsets = UIEdgeInsets(top: 0, left: 0, bottom: 44, right: 0) updateAdditionalSafeAreaInsets()
pasteConfiguration = UIPasteConfiguration(forAccepting: CompositionAttachment.self) pasteConfiguration = UIPasteConfiguration(forAccepting: CompositionAttachment.self)
@ -125,6 +128,9 @@ class ComposeHostingController: UIHostingController<ComposeContainerView> {
return toolbar return toolbar
} }
private func updateAdditionalSafeAreaInsets() {
additionalSafeAreaInsets = UIEdgeInsets(top: 0, left: 0, bottom: toolbarHeight + keyboardHeight, right: 0)
}
@objc private func keyboardWillShow(_ notification: Foundation.Notification) { @objc private func keyboardWillShow(_ notification: Foundation.Notification) {
keyboardWillShow(accessoryView: inputAccessoryToolbar, notification: notification) keyboardWillShow(accessoryView: inputAccessoryToolbar, notification: notification)
@ -135,6 +141,17 @@ class ComposeHostingController: UIHostingController<ComposeContainerView> {
accessoryView.alpha = 1 accessoryView.alpha = 1
accessoryView.isHidden = false accessoryView.isHidden = false
// on iOS 14, SwiftUI safe area automatically includes the keyboard
if #available(iOS 14.0, *) {
} else {
let userInfo = notification.userInfo!
let frame = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as! CGRect
// temporarily reset add'l safe area insets so we can access the default inset
additionalSafeAreaInsets = .zero
keyboardHeight = frame.height - view.safeAreaInsets.bottom - accessoryView.frame.height
updateAdditionalSafeAreaInsets()
}
} }
@objc private func keyboardWillHide(_ notification: Foundation.Notification) { @objc private func keyboardWillHide(_ notification: Foundation.Notification) {
@ -167,6 +184,13 @@ class ComposeHostingController: UIHostingController<ComposeContainerView> {
} completion: { (finished) in } completion: { (finished) in
accessoryView.alpha = 1 accessoryView.alpha = 1
} }
// on iOS 14, SwiftUI safe area automatically includes the keyboard
if #available(iOS 14.0, *) {
} else {
keyboardHeight = 0
updateAdditionalSafeAreaInsets()
}
} }
@objc private func keyboardDidHide(_ notification: Foundation.Notification) { @objc private func keyboardDidHide(_ notification: Foundation.Notification) {
@ -242,17 +266,6 @@ class ComposeHostingController: UIHostingController<ComposeContainerView> {
} }
extension ComposeHostingController {
struct EnvironmentWrappingView<Content: View, EnvironmentObject: ObservableObject>: View {
let content: Content
let environmentObject: EnvironmentObject
var body: some View {
content.environmentObject(environmentObject)
}
}
}
extension ComposeHostingController: ComposeUIStateDelegate { extension ComposeHostingController: ComposeUIStateDelegate {
var assetPickerDelegate: AssetPickerViewControllerDelegate? { self } var assetPickerDelegate: AssetPickerViewControllerDelegate? { self }