Skip to content
This repository was archived by the owner on Jun 17, 2023. It is now read-only.

Commit d7c52c4

Browse files
author
Amir Khorsandi
committed
Handle keyboard frame on iPad
1 parent 89027fd commit d7c52c4

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Samples/PagingLayoutSamples/Modules/LayoutDesigner/LayoutDesignerViewController.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ class LayoutDesignerViewController: UIViewController, ViewModelBased, NibBased {
3838
super.viewDidLoad()
3939
configureViews()
4040
setOptionsList()
41+
registerKeyboardNotifications()
42+
}
43+
44+
deinit {
45+
NotificationCenter.default.removeObserver(self)
4146
}
4247

4348

@@ -132,6 +137,27 @@ class LayoutDesignerViewController: UIViewController, ViewModelBased, NibBased {
132137
optionsTableView.optionViewModels = viewModel.optionViewModels
133138
}
134139

140+
private func registerKeyboardNotifications() {
141+
let notificationCenter = NotificationCenter.default
142+
notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardWillHideNotification, object: nil)
143+
notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
144+
}
145+
146+
@objc private func adjustForKeyboard(notification: Notification) {
147+
guard let keyboardValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return }
148+
149+
let keyboardScreenEndFrame = keyboardValue.cgRectValue
150+
let keyboardViewEndFrame = view.convert(keyboardScreenEndFrame, from: view.window)
151+
var contentInset = optionsTableView.contentInset
152+
153+
if keyboardViewEndFrame.minY < optionsTableView.frame.maxY {
154+
contentInset.bottom = optionsTableView.frame.maxY - keyboardViewEndFrame.minY - 8
155+
} else {
156+
contentInset.bottom = 8
157+
}
158+
optionsTableView.contentInset = contentInset
159+
}
160+
135161
}
136162

137163

0 commit comments

Comments
 (0)