3

I would like to know if it is somehow possible to get the keyboard size of the device that my application is currently running on without any notification, because I'd like to place a Button exactly at that point inside of my View where the keyboard would end. There would be the possibility to change the position when the keyboard appears, but as I said before I want it to be there before it animates in.

Otherwise maybe there is any static size of the iOS keyboards, like e.g. 1/3 of the screen height of the device or something?

This is what I am trying to achieve:

Screenshot of hardcoded one on iPhone XS Max:

Screenshot of hardcoded one on iPhone XS Max

Thank you!

Ehsan Mohammadi
  • 1,168
  • 1
  • 15
  • 21
F. Leeser
  • 31
  • 1
  • Have you checked the answers to this question? Could any of those guide you? If not - and because you want this "login"button to not be visible when the keyboard isn't visible, could you show some code to help us? In particular, your view and your model. Thanks! https://stackoverflow.com/questions/56716311/how-to-show-complete-list-when-keyboard-is-showing-up-in-swiftui –  Jul 29 '19 at 13:39
  • @dfd Every view should be visible at any point of time, also the Login button should be visible. It only needs a padding of the height of the keyboard from the bottom of the device. That's what I try to achieve. – F. Leeser Jul 29 '19 at 13:44
  • Okay, one clarification question: Are you wanting to (a) NOT have your view adjust to the keyboard and (b) have the login button remain static when it appears? –  Jul 29 '19 at 13:47
  • Nevermind, you already stated that. My bad. :-) The idea of SwiftUI is "fluid" (also "reactive", but not in your case). I'll wager (but there are others much better at this than me) that if you have a way in `UIKit` to achieve this, that's the route to go. I think the easiest route to go is to use `GeometryReader` to get the size of the parent, and place it proportionally - like you said, 1/3 of the screen height. –  Jul 29 '19 at 13:51
  • @dfd I could just place it 1/3 of the parent above the bottom, but that would not be what I wanna achieve. So, you don't know how to get the exact size of the keyboard or calculate it? – F. Leeser Jul 29 '19 at 14:57
  • Have you found a solution to this by any chance? – mota Mar 30 '22 at 08:14

1 Answers1

0

I have done it creating an extension for Publisher. However, it uses NotificationCenter

extension Publishers {
    static var keyboardHeight: AnyPublisher<CGFloat, Never> {
        let willShow = NotificationCenter.default.publisher(for: UIApplication.keyboardWillShowNotification)
            .map { $0.keyboardHeight }
        
        let willHide = NotificationCenter.default.publisher(for: UIApplication.keyboardWillHideNotification)
            .map { _ in CGFloat(0) }
        
        return MergeMany(willShow, willHide)
            .eraseToAnyPublisher()
    }
}

Also, you need to create a state variable for keyboardHeight

@State private var keyboardHeight: CGFloat = 0

Finally, you can observe the change in height using:

Text("")
       .onReceive(Publishers.keyboardHeight) {
                            self.keyboardHeight = $0
                        }

This is the approach I used for getting the height.

Dharman
  • 30,962
  • 25
  • 85
  • 135