Reading time: 2 min

Detecting if a text field gained or lost focus is not entirely straightforward in SwiftUI.

TextField constructor has a parameter named onEditingChanged, but it triggers only when the user taps the return key in the virtual keyboard, not when the focus is actually lost.

Even worse, SecureField doesn't have such a parameter, and there's no way to know if the user is interacting with the view!

Continue Reading...

Reading time: 1 min

When implementing a custom TextField by wrapping a UITextField in UIViewRepresentable (just like we did in our custom SecureField that tracks focus change, you need to be careful as its size normally grows with its content.

Continue Reading...

Reading time: 1 min

A common issue in SwiftUI is that, when the keyboard appears, it covers up a part of your UI. This is especially problematic if it overlaps the exact TextField you're editing, so that you can't even see what you're typing!

Take this credit card input field at the bottom of a screen:

If you open the keyboard up, it gets covered up completely:

Use the following code to make your views adjust their bottom padding according to keyboard visibility.

Continue Reading...

Reading time: 2 min

This recipe shows how to add a TextField to a SwiftUI alert dialog.

Continue Reading...