Search code examples
swiftuitableviewswiftuiswiftui-formswiftui-datepicker

SwiftUI: Detected a case where constraints ambiguously suggest a height of zero for a table view cell's content view


How can I fix the reason why the warning appears?

struct ContentView: View {

    @State private var selectedDate: Date = Date()

    var body: some View {
        Form {
            DatePicker("Date", selection: $selectedDate, displayedComponents: .date)
        }
    }
}

Solution

  • A workaround solution would be to use a List View instead of a Form. It all depends on what you want to put in your Form. For demonstration purpose, using a List, your code would look like this:

    struct ContentView: View {
    
        @State private var selectedDate: Date = Date()
    
        var body: some View {
            List {
                DatePicker("Date", selection: $selectedDate, displayedComponents: .date)
            }
            .listStyle(InsetGroupedListStyle())
        }
    }
    

    The above gives the same visual effect (UI) as using a List, and no warning is shown.

    Because everything is working correctly with your Form, I don't really see the need to change your Form to a List just to avoid the logs.