-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathKeyboardNotificationView.swift
More file actions
24 lines (22 loc) · 1.04 KB
/
Copy pathKeyboardNotificationView.swift
File metadata and controls
24 lines (22 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
struct KeyboardNotificationView: View {
@ObservedObject var viewModel = DemoViewModel()
@State private var bottomPadding: CGFloat = 0
var body: some View {
List(viewModel.rowModels, rowContent: DemoRow.init)
.padding(EdgeInsets(top: 0, leading: 0, bottom: bottomPadding, trailing: 0))
.onReceive(NotificationCenter.default.publisher(for: UIWindow.keyboardWillShowNotification),
perform: updateFrame)
.onReceive(NotificationCenter.default.publisher(for: UIWindow.keyboardWillHideNotification),
perform: updateFrame)
.animation(.default)
}
private func updateFrame(_ notification: Notification) {
let nsValue = (notification.userInfo?[UIWindow.keyboardFrameEndUserInfoKey] as? NSValue)
let keyboardHeight = nsValue?.cgRectValue.height ?? 0
if notification.name == UIWindow.keyboardWillShowNotification {
bottomPadding = keyboardHeight
} else {
bottomPadding = 0
}
}
}