4

I have a date picker inside a bottom sheet view.

When the bottom sheet opens, the first time I choose a day, the date picker height changes. P.S: If I set a custom height for the picker using .frame, the date picker just becomes smooshed inside the frame.

enter image description here

    var body: some View {
    VStack {
        HStack {
            Text("date_picker_title".ld())
                .font(.custom(Font.rRegular, size: 12))
                .padding()
            Spacer()
        }
        HStack {
            Text(date.prettyDate)
                .font(.custom(Font.rMedium, size: 28))
            Spacer()
            Text(date.prettyHour)
                .font(.custom(Font.rMedium, size: 28))
        }.padding(.horizontal)
        Seperator(cellHeight: 0).frame(height: 2)

        Spacer().frame(height: 6)
        DatePicker("", selection: $date, in: Date()..., displayedComponents: .date)
            .datePickerStyle(.graphical)
            .labelsHidden()
            .tint(Color.appPurple)
            .padding(.horizontal)
            .padding(.bottom, -10)
        Seperator(cellHeight: 0).frame(height: 2)
        HStack {
            Text("date_picker_enter_time".ld())
                .font(.custom(Font.rRegular, size: 12))
            DatePicker("", selection: $date, displayedComponents: .hourAndMinute)
                .labelsHidden()
                .tint(Color.appPurple)
            Spacer()
        }.padding()
        
        HStack {
            Button {
                completion(nil)
            } label: {
                Text("date_picker_cancel".ld())
                    .font(.custom(Font.rRegular, size: 14))
                    .foregroundColor(Color.appPurple)
            }
            Spacer()
            Button {
                completion(date)
            } label: {
                Text("date_picker_ok".ld())
                    .font(.custom(Font.rRegular, size: 14))
                    .foregroundColor(Color.appPurple)
            }
        }.padding(.horizontal)
        Spacer().frame(height: 50)
    }
    .frame(maxWidth: .infinity)
    .background(Color.appLightPurple)
    .padding(.bottom, -50)
}
Vadim F.
  • 881
  • 9
  • 21

2 Answers2

3

I was having a similar problem and getting this console warning: "UICalendarView's height is smaller than it can render its content in; defaulting to the minimum height"

I found this answer to set the frame width. It's not optimal but seems to be solving my problem.

DatePicker(selection: $date, displayedComponents: .date)
   .datePickerStyle(.graphical)
   .frame(width: 320) // Bugfix for AutoLayout-Issue
Dava Brown
  • 41
  • 5
0

I have no idea why but setting the ID of the Date picker to the date being selected seems to fix this jumping issue:

    DatePicker("title", selection: $fromDate, displayedComponents: .date)
        .datePickerStyle(.graphical)
        .id(fromDate) // Magically stops it from jumping in size on first selection
Jonathan.
  • 53,997
  • 54
  • 186
  • 290