Search code examples
iosswiftuikitios15

How to workaround broken UIKeyboardLayoutGuide with Navigation Views?


iOS 15 added the new and shiny UIKeyboardLayoutGuide to be able to put constraints on views that need to react with the keyboard.

This works great in a single view, but when you have constraints set up in View A and push View B onto the stack the keyboard layout guide suddenly gets a size of CGRect.zero (in View A).

When returning back to View A the keyboard layout guide frame is still .zero and thus the constraint between the keyboard layout guide top and "SomeView".bottom for example is broken.

To showcase the issue I added a green view to my tracking controller and a bottom constraint to the keyboard layout guide top anchor, like so:

let kbView = UIView()
kbView.backgroundColor = .green
kbView.translatesAutoresizingMaskIntoConstraints = false

self.view.addSubview(kbView)

kbView.widthAnchor.constraint(equalToConstant: 200).isActive = true
kbView.heightAnchor.constraint(equalToConstant: 100).isActive = true
kbView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true

kbView.bottomAnchor.constraint(equalTo: self.view.keyboardLayoutGuide.topAnchor).isActive = true

You can find the whole project in the link below on Github.

How can we workaround this issue and did someone else also experience this issue?
My current workaround is to write my own layout guide, which just works - and is compatible with iOS < 15.

Really, this can't be true (?!), given the fact that this is a new and shiny feature...
Maybe I overlooked something...

Anyway: Here is the demo project showcasing the issue.


Solution

  • Seems like you might just need to raise the threshold for those features to 15.4 https://twitter.com/kharrison/status/1506947828773498881