SwiftUI TextField: Keyboard does not show up on tapped

Viewed 887

I have a simple TextField in a HStack together with a button

        HStack {
            TextField("Level Name", text: $levelName)
                .font(.title)
                .padding(10)
                .background(
                    RoundedRectangle(cornerRadius: 15)
                        .strokeBorder(Color.primary.opacity(0.5), lineWidth: 3))
            
            Button(action: {

            }) {
                Text("Submit").font(.title)
            }
        }

Clicking on the textfield does not display the keyboard on my emulator, giving the console warning:

[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:0x600002bba8f0 h=--& v=--& _UIButtonBarButton:0x7fdbcbc52e90.height == 0 (active)>", "<NSLayoutConstraint:0x600002bb4050 _UIUCBKBSelectionBackground:0x7fdbcbc53890.bottom == _UIButtonBarButton:0x7fdbcbc52e90.bottom - 6 (active)>", "<NSLayoutConstraint:0x600002bbff70 V:|-(6)-[_UIUCBKBSelectionBackground:0x7fdbcbc53890] (active, names: '|':_UIButtonBarButton:0x7fdbcbc52e90 )>" )

Will attempt to recover by breaking constraint <NSLayoutConstraint:0x600002bb4050 _UIUCBKBSelectionBackground:0x7fdbcbc53890.bottom == _UIButtonBarButton:0x7fdbcbc52e90.bottom - 6 (active)>

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.

1 Answers

Your code works. Keyboard shows up. Running iOS 15.1.

struct TestView: View {
    
    @State var levelName = ""
    
    var body: some View {
           
        HStack {
            TextField("Level Name", text: $levelName)
                .font(.title)
                .padding(10)
                .background(
                    RoundedRectangle(cornerRadius: 15)
                        .strokeBorder(Color.primary.opacity(0.5), lineWidth: 3))
            
            Button(action: {
                
            }) {
                Text("Submit").font(.title)
            }
        }
        
    }
    
}
Related