From d7c52c4c180f0cd869a9a6747da25c7f78d4cc45 Mon Sep 17 00:00:00 2001 From: Amir Khorsandi Date: Sun, 5 Jul 2020 15:47:33 +0200 Subject: [PATCH] Handle keyboard frame on iPad --- .../LayoutDesignerViewController.swift | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/Samples/PagingLayoutSamples/Modules/LayoutDesigner/LayoutDesignerViewController.swift b/Samples/PagingLayoutSamples/Modules/LayoutDesigner/LayoutDesignerViewController.swift index fedf729..9f48436 100644 --- a/Samples/PagingLayoutSamples/Modules/LayoutDesigner/LayoutDesignerViewController.swift +++ b/Samples/PagingLayoutSamples/Modules/LayoutDesigner/LayoutDesignerViewController.swift @@ -38,6 +38,11 @@ class LayoutDesignerViewController: UIViewController, ViewModelBased, NibBased { super.viewDidLoad() configureViews() setOptionsList() + registerKeyboardNotifications() + } + + deinit { + NotificationCenter.default.removeObserver(self) } @@ -132,6 +137,27 @@ class LayoutDesignerViewController: UIViewController, ViewModelBased, NibBased { optionsTableView.optionViewModels = viewModel.optionViewModels } + private func registerKeyboardNotifications() { + let notificationCenter = NotificationCenter.default + notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardWillHideNotification, object: nil) + notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardWillChangeFrameNotification, object: nil) + } + + @objc private func adjustForKeyboard(notification: Notification) { + guard let keyboardValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return } + + let keyboardScreenEndFrame = keyboardValue.cgRectValue + let keyboardViewEndFrame = view.convert(keyboardScreenEndFrame, from: view.window) + var contentInset = optionsTableView.contentInset + + if keyboardViewEndFrame.minY < optionsTableView.frame.maxY { + contentInset.bottom = optionsTableView.frame.maxY - keyboardViewEndFrame.minY - 8 + } else { + contentInset.bottom = 8 + } + optionsTableView.contentInset = contentInset + } + }