UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How do I make iOS and iPadOS happy with a GraphicalDatePicker in a popover?

Forums > SwiftUI

I'm trying to present a GraphicalDatePicker in a popover from a Form. In iOS this defaults to a sheet and works fine. In iPadOS this is a disaster. The popover appears in the middle of my form row instead of pointing at the button I'm calling it from, and worse still, it collapses in width and height as if there is nothing there at all. I tried adding .frame(width: 320, height: 400) which does expand my popover and lets me see the GraphicalDatePicker in iPadOS (still in the center of the form row, but at least I can see it now and it functions)... but then this is a complete disaster in iOS. Any ideas?

import SwiftUI

struct DateView: View {
    let dateFormatter: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateStyle = .medium
        return formatter
    }()

    @State private var selectedDate = Date()
    @State private var showingPopover = false

    var body: some View {
        Form {
            HStack {
                Text("Date")
                Spacer()
                Button(action: {
                    showingPopover = true
                }) {
                    HStack {
                        Text("\(selectedDate, formatter: dateFormatter)")
                        .foregroundColor(.secondary)
                    Image(systemName: "calendar")
                        .imageScale(.large)
                    }
                }
                .layoutPriority(1)
                .buttonStyle(BorderlessButtonStyle())
            }
            .popover(isPresented: $showingPopover) {
                NavigationView {
                    VStack {
                        DatePicker("Date", selection: $selectedDate, displayedComponents: .date)
                            .labelsHidden()
                            .datePickerStyle(GraphicalDatePickerStyle())
                            .padding()

                        Spacer()
                    }
                    .navigationBarTitle(Text("Select Date"), displayMode: .inline)
                    .toolbar {
                        ToolbarItem(placement: .navigationBarLeading) {
                            Button("Done") {
                                showingPopover = false
                            }
                        }
                    }
                }
                .accentColor(.red)
                //.frame(width: 320, height: 400)
            }
        }
        .accentColor(.red)
    }
}

struct DateView_Previews: PreviewProvider {
    static var previews: some View {
        DateView()
    }
}

2      

Hi Mike,

I do not understand why you trying to recreate some tht is done with DatePicker

struct ContentView: View {
    @State private var selectedDate = Date()

    var body: some View {
        Form {
            DatePicker("Date", selection: $selectedDate)
                .accentColor(.red)
        }
        .padding()
    }
}

When tap on the date it brings you a GraphicalDatePickerStyle() in popover view and even blurs the background if you want the date only or time then add displayedComponents: .date or displayedComponents: .hourAndMinute. This will then use the correct format for time for each locale and each device.

2      

Because...

  1. I don't like the blurring background effect in iOS (which is inconsistent with the rest of the UI)
  2. The button it places in my form can't seem to stay aligned to the trailing edge
  3. The button it places in my form can't seem to decide if it wants to use a medium or short date format and randomly alternates
  4. Sometimes the text is shorter than (and right aligned) in the rounded rectangle background
  5. There are all sorts of sizing errors reported in Xcode any time the DatePicker opens in iOS and in iPadOS
  6. I don't really like the button style in my form with its rounded rectangle background and want something with a cleaner appearance
  7. There is nothing to indicate to the user how to get rid of it after they make a selection

That said, I do really like the Graphical Date Picker calendar itself in iOS / iPadOS. This is the best date picker calendar that Apple has ever made. It looks fantastic and is so much better in form and function than the pathetic little thing that passes as a date picker in macOS. Apple would do well to make this beautiful date picker calendar available on macOS.

2      

Looks like my best option is this - at least until Apple fixes some of the issues I mentioned with DatePicker (and yes, I will be putting the 2 additional views in seperate SwiftUI files and my class into a seperate Swift file and not all in one single long bit of code like I'm sharing here). Of course, if anyone knows of any better option or wants to offer any improvements... I'd be happy to see that.

import SwiftUI

struct DateView: View {
    @StateObject var selectedDate = PassDateObject(Date())

    let dateFormatter: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateStyle = .medium
        return formatter
    }()

    @State private var showingPopover = false

    var body: some View {
        Form {
            HStack {
                Text("Date")
                Spacer()
                Button(action: {
                    showingPopover = true
                }) {
                    HStack {
                        Text("\(selectedDate.date, formatter: dateFormatter)")
                        .foregroundColor(.secondary)
                    Image(systemName: "calendar")
                        .imageScale(.large)
                    }
                }
                .layoutPriority(1)
                .buttonStyle(BorderlessButtonStyle())
            }
            .popover(isPresented: $showingPopover, attachmentAnchor: .point(.bottomTrailing), arrowEdge: .trailing) {
                if UIDevice.current.userInterfaceIdiom == .pad {
                    iPadOSDatePicker().environmentObject(selectedDate)
                } else {
                    iOSDatePicker().environmentObject(selectedDate)
                }
            }
        }
        .accentColor(.red)
    }
}

class PassDateObject: ObservableObject {
    @Published var date: Date
    init(_ date: Date) {
        self.date = date
    }
}

struct iPadOSDatePicker: View {
    @EnvironmentObject var selectedDate: PassDateObject
    @Environment(\.presentationMode) var presentationMode
    var body: some View {
        NavigationView {
            HStack {
                DatePicker("Date", selection: $selectedDate.date, displayedComponents: .date)
                    .labelsHidden()
                    .datePickerStyle(GraphicalDatePickerStyle())
                    .padding()

                Spacer()
                Spacer()
            }
            .navigationBarTitle(Text("Select Date"), displayMode: .inline)
            .toolbar {
                ToolbarItem(placement: .navigationBarLeading) {
                    Button("Done") {
                        presentationMode.wrappedValue.dismiss()
                    }
                }
            }
        }
        .accentColor(.red)
        .frame(width: 340, height: 400)
    }
}

struct iOSDatePicker: View {
    @EnvironmentObject var selectedDate: PassDateObject
    @Environment(\.presentationMode) var presentationMode
    var body: some View {
        NavigationView {
            VStack {
                DatePicker("Date", selection: $selectedDate.date, displayedComponents: .date)
                    .labelsHidden()
                    .datePickerStyle(GraphicalDatePickerStyle())
                    .padding()

                Spacer()
            }
            .navigationBarTitle(Text("Select Date"), displayMode: .inline)
            .toolbar {
                ToolbarItem(placement: .navigationBarLeading) {
                    Button("Done") {
                        presentationMode.wrappedValue.dismiss()
                    }
                }
            }
        }
        .accentColor(.red)
    }
}

struct DateView_Previews: PreviewProvider {
    static var previews: some View {
        DateView()
    }
}

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.