swift – Does `UIResponder.keyboardWillShowNotification` provde incorrect height on iOS devices with curved screens?


The extra height comes from the container’s safe area, the area that contains the “home swipe bar” on iPhone 15 and other models.

keyboardFrameEndUserInfoKey includes the container’s safe area too, but you only ignored the .keyboard safe area, and that’s why the red area appears higher than what you expect.

One workaround is to ignore all safe areas depending on whether the keyboard is showing.

// replace the existing ignoresSafeArea modifier with this:
.ignoresSafeArea(keyboardHeight == 0 ? [] : .all)

Alternatively, you can avoid using NotificationCenter, and do this in pure SwiftUI. All you need is to read the container safe area, and the total safe area, and subtract one from the other.

GeometryReader { total in // this is the total safe area, because no safe area is ignored
    GeometryReader { container in // this is the container safe area, because we ignored the keyboard safe area
        VStack {
            TextField("Test", text: $text)
            Color.red
                // we subtract it here:
                .frame(height: total.safeAreaInsets.bottom - container.safeAreaInsets.bottom)
        }
        .frame(maxHeight: .infinity, alignment: .bottom)
    }
    .ignoresSafeArea(.keyboard)
}

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img