Software keyboard generates errors in XCode 16

Hi all,
After iOS foundations, databases and networking, I am building an App that has several TextFields and TextEditors. I am using
‘’’ @FocusState var fieldIsFocused ‘’’ where needed. I noticed that when a software keyboard is called from a TextField, the return key dismisses the keyboard. When it’s a TextEditor, the return key makes a new line (makes total sense). To dismiss the keyboard for this case I Googled, got a solution and added a Toolbar to the Keyboard with the following modifier on the TextField:
‘’’
.toolbar {

ToolbarItemGroup(placement: .keyboard) {
                                Spacer()
                                
                                Button {
                                    fieldIsFocused = false
                                    
                                } label : {
                                    Text ("Done")
                                        
                                }
                            }
                        }

‘’’
This works and does not crash the app, but when I run in the simulator I get two errors in the console:

  1. (This one is colored red, I assume that’s bad!)
    Invalid frame dimension (negative or non-finite).
  2. This one is yellow:
    Unable to simultaneously satisfy constraints. A long one, happy to paste the whole error if required, it contains “<NSAutoresizingMaskLayoutConstraint:0x6000021ff3e0 h=–& v=–& _UIToolbarContentView:0x105d61250.width == 0 (active)>”,

After hours with Stack overflow and ChatGPT, I have not found a solution.
Can anybody help?

I am close to launch, and concerned that my App will be refused in the App store if I dont fix these.

When you submit your App to the App Store, the archive you submit does not contain any source code so the App Store staff don’t see what goes on in Xcode and what is displayed in the debug console. Essentially they test the App for functionality and ensure that it meets Apples interface guidelines (and other criteria) so I would not be concerned about the constraints message you are seeing. As for the frame dimension can you provide some example code for the View that the compiler is getting cranky about? Check your .frame() modifiers (if you have any) that might be causing that warning in the debug console.

Thanks Chris,
I now made a super minimal version with nothing but the text editor and the keyboard toolbar. It still gives those errors. Code below. If I remove the toolbar and just go with a “vanilla” keyboard, it doesnt give those errors.

Second problem: with or without the toolbar modifier, it gives the following error as soon as I tap and hold a key on the software keyboard (this i have seen before and looked for solutions but didnt solve it. Doesn’t crash the app but I wonder if this is known bug and if there is a solution?):
‘’‘Error: this application, or a library it uses, has passed an invalid numeric value (NaN, or not-a-number) to CoreGraphics API and this value is being ignored. Please fix this problem.’‘’

here is the code that generates these erorrs, and the ones that depend on the toolbar:
‘’'import SwiftUI

struct KeyboardExample: View {

@State private var usertext = "Oh, the blank page!"
@FocusState private var fieldIsFocused: Bool

var body: some View {
    
    VStack {
        
        Text("This is a Text Editor")
            .font(.title2)
        
        TextEditor(text: $usertext)
            .padding()
            .focused($fieldIsFocused)
        
            .toolbar {
                
                ToolbarItemGroup(placement: .keyboard) {
                    Spacer()
                    
                    Button {
                        fieldIsFocused = false
                        
                    } label : {
                        Text ("Done")
                            .bold()
                    }
                }
            }
    }
}

} ‘’’

Looks like a bug to me but more importantly I noticed that on the initial run to the simulator, no button appeared on the keyboard. The next time I complied and ran the code the button appeared and so did the warnings/errors.

I found a number of examples online for setting up that keyboard button and they all largely agree with what you have done. I copied one listed from Paul Hudson and although he used a very simplified Button I still got those warnings in the Xcode console and that included wrapping the Spacer and Button in a HStack thinking that it might make a difference.

struct KeyboardExample: View {

    @State private var usertext = "Oh, the blank page!"
    @FocusState private var fieldIsFocused: Bool

    var body: some View {

        VStack {

            Text("This is a Text Editor")
                .font(.title2)

            TextEditor(text: $usertext)
                .padding()
                .focused($fieldIsFocused)
                .toolbar {
                    ToolbarItemGroup(placement: .keyboard) {
                        HStack {
                            Spacer()
                            Button("Done") {
                                fieldIsFocused = false
                            }
                        }
                    }
                }
        }
    }
}

I tried moving the .toolbar modifier to the closing VStack brace but that made no difference either. ie like this:

struct KeyboardExample: View {

    @State private var usertext = "Oh, the blank page!"
    @FocusState private var fieldIsFocused: Bool

    var body: some View {

        VStack {

            Text("This is a Text Editor")
                .font(.title2)

            TextEditor(text: $usertext)
                .padding()
                .focused($fieldIsFocused)
        }
        .toolbar {
            ToolbarItemGroup(placement: .keyboard) {
                HStack {
                    Spacer()
                    Button("Done") {
                        fieldIsFocused = false
                    }
                }
            }
        }
    }
}

Hi Chris,
thank you for taking a look at this.
Should I do anything further to try to solve this? One thing I thought of was to drop the keyboard toolbar entirely and just make a regular button that appears when the keyboard appears. This seems a bit makeshift but I guess I can do it that way.

Did you also get the same errors as I did when you tap and hold any key of the keyboard?
“”“Error: this application, or a library it uses, has passed an invalid numeric value (NaN, or not-a-number) to CoreGraphics API and this value is being ignored. Please fix this problem.”“”
Thank you for your time!
Best wishes,
Leonie

Hi Leonie,

I ran the code on my real device and the Constraint use and the Invalid frame dimension (negative or non-finite). issue was present. They don’t affect the App operation.

You could do away with the keyboard toolbar and have a button in the Navigation bar at the top (assuming you are using a NavigationStack) and only show the button when the focus is on the TextEditor. Like this for example

struct KeyboardExample: View {

    @State private var usertext = "Oh, the blank page!"
    @FocusState private var fieldIsFocused: Bool

    var body: some View {

        NavigationStack {
            VStack {

                Text("This is a Text Editor")
                    .font(.title2)

                TextEditor(text: $usertext)
                    .padding()
                    .focused($fieldIsFocused)
            }
            .navigationTitle(Text("Keyboard Example"))
            .toolbar {
                if fieldIsFocused {
                    ToolbarItemGroup(placement: .topBarTrailing) {
                        Button("Done") {
                            fieldIsFocused = false
                        }
                    }
                }
            }
        }
    }
}

When doing it this way there are no errors/warnings in the debug console.

I didn’t get the .......Please fix this problem issue that you are getting.

Hi Chris,

thanks for your suggestion, I will give that a try.

I described the other error (… Please fix this problem) inaccurately, apologies. This one comes whenever I have the keyboard showing and then tap again in the text field.

Here is the error: Error: this application, or a library it uses, has passed an invalid numeric value (NaN, or not-a-number) to CoreGraphics API and this value is being ignored. Please fix this problem.

Could you see if you get that in your version as well?

I can suppress it with the following modifier on the textField, but I am not sure if this is the best way to do it.
‘’’
.onLongPressGesture(minimumDuration: 0.0){
fieldIsFocused = true
}
‘’’

Thanks!

all the best

Leonie

Hi Leonie,

With your original code I wasn’t getting that error/warning on the simulator or even on my real device. It’s possible that the issue is somewhere else in your App rather than this view.

Hi Chris,
thank you for checking that. Good to know but very odd! I made a new App, containing only my original code in the content view. I still get those errors when I tap the text field when the keyboard is open. I will work around it I guess.
best,
Leonie

1 Like