In our app we're hiding and showing things based on whether the keyboard is on the screen or not so we're observing:
- UIResponder.keyboardWillShowNotification
- UIResponder.keyboardWillHideNotification
And now that we've got hte iPhone Duo in house, I've been testing on there and noticed that those events are firing when the device is open and rotating? Is this by design? There's no keyboard on screen, but I'm receiving the events?
This behavior is not showing on the other iPhone/iPad devices.
To double-check I created a sample app and I'm seeing the same behavior 😔
enum KeyboardNotification {
case willShow
case didShow
case willHide
case didHide
}
extension KeyboardNotification: CustomStringConvertible {
var description: String {
switch self {
case .willShow:
"Will Show"
case .didShow:
"Did Show"
case .willHide:
"Will Hide"
case .didHide:
"Did Hide"
}
}
}
struct NotificationEntry {
let notification: KeyboardNotification
let date: Date
init(_ notification: KeyboardNotification) {
self.notification = notification
self.date = Date()
}
}
struct TestView: View {
@State private var entries: [NotificationEntry] = []
private let dateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .short
formatter.timeStyle = .short
return formatter
}()
var body: some View {
ZStack {
if !entries.isEmpty {
List {
ForEach(entries, id: \.date) { entry in
VStack(alignment: .leading) {
Text(String(describing: entry.notification))
Text(dateFormatter.string(from: entry.date))
.font(.caption)
.foregroundStyle(.gray)
}
}
}
} else {
VStack(alignment: .leading) {
Text("No Keyboard Events")
Text("Rotate the device to test.")
.font(.caption)
.foregroundStyle(.gray)
}
}
}
.onReceive(NotificationCenter.default.publisher(for: UIResponder.keyboardWillShowNotification)) { _ in
entries.append(NotificationEntry(.willShow))
}
.onReceive(NotificationCenter.default.publisher(for: UIResponder.keyboardDidShowNotification)) { _ in
entries.append(NotificationEntry(.didShow))
}
.onReceive(NotificationCenter.default.publisher(for: UIResponder.keyboardWillHideNotification)) { _ in
entries.append(NotificationEntry(.willHide))
}
.onReceive(NotificationCenter.default.publisher(for: UIResponder.keyboardDidHideNotification)) { _ in
entries.append(NotificationEntry(.didHide))
}
}
}
In my test app, the events will not always fire but after messing with it for a little bit it will eventually get in the state where it the events fire.
Sometimes I will additionally see them when bringing down the notification center, or slightly going to the app switcher.