SwiftUI ToolbarItem Constraints Error

In this simple Hello World app (Xcode 14.3, iOS 14.4.1), adding a Toolbar item above the keyboard leads to constraints errors in the log when you tap the TextField. It does not seem to lead to any obvious errors in the UI.

Anyone have ideas on how to work around this?

struct ContentView: View {
    
    @State private var foodName: String = ""
    
    var body: some View {
        VStack {
            TextField("test", text: $foodName)
        }
        .padding()
        .toolbar {
            ToolbarItem(placement: .keyboard) {
                Text("Test")
            }
        }
    }
}
 [LayoutConstraints] Unable to simultaneously satisfy constraints.
	Probably at least one of the constraints in the following list is one you don't want. 
	Try this: 
		(1) look at each constraint and try to figure out which you don't expect; 
		(2) find the code that added the unwanted constraint or constraints and fix it. 
	(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x28137d2c0 h=--& v=--& _UIToolbarContentView:0x143609e30.width == 0   (active)>",
    "<NSLayoutConstraint:0x2813708c0 H:|-(16)-[_UIButtonBarStackView:0x143608ab0]   (active, names: '|':_UIToolbarContentView:0x143609e30 )>",
    "<NSLayoutConstraint:0x281370910 H:[_UIButtonBarStackView:0x143608ab0]-(16)-|   (active, names: '|':_UIToolbarContentView:0x143609e30 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x281370910 H:[_UIButtonBarStackView:0x143608ab0]-(16)-|   (active, names: '|':_UIToolbarContentView:0x143609e30 )>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
Post not yet marked as solved Up vote post of pilotcoder Down vote post of pilotcoder
985 views

Replies

Hi. Did you find the fix for this? I'm having the same issue

List(viewModel.points.indices, id: \.self) { index in
    HStack(spacing: 0) {
        Text(viewModel.points[index].name)
            .modifier(RCTextModifier(style: .listMainRegular))
            .frame(width: 120, alignment: .leading)

        Spacer()

        TextField(LocalizedKeys.ScoreCreation.points, text: $viewModel.points[index].score)
            .modifier(RCTextFieldModifier(style: .number))
            .frame(width: 120, alignment: .leading)

        Spacer()
    }
    .listRowBackground(Color.clear)
    .listRowInsets(EdgeInsets(top: 0, leading: viewModel.screenWidth * 0.2, bottom: 0, trailing: viewModel.screenWidth * 0.2))
}
.listStyle(.plain)
.toolbar {
    ToolbarItemGroup(placement: .keyboard) {
        Spacer()
        Button(LocalizedKeys.Common.done) {
            UIApplication.shared.endEditing()
        }
    }
}
  • I too am having the same issue and it bugs me. Have you fixed it?

Add a Comment

It’s a warning, not an error. It says your toolbar content view is 0 in width but you’re also trying to stretch it be 16 from leading and trailing edge. I wouldn’t care about it unless something breaks... 🤓

  • I'm also getting this exact warning and it bugs me, I would definitely be interested in a fix

Add a Comment

Has any solution been found?