79597618

Date: 2025-04-29 04:48:20
Score: 0.5
Natty:
Report link

What’s happening:

Quick Repro of Your Problem:

In your code:

DatePicker(selection: $viewModel.selectedDate, displayedComponents: .date) {}     .labelsHidden()     .allowsHitTesting(true)     .contentShape(Rectangle())     .opacity(0.011) // <--- making it almost invisible

That .opacity(0.011) is now a blocker in iOS 18.

Commenting it out, you said, brings back the date picker UI — because the system now requires a visibly interactive surface.

Solution 1: Use .opacity(0.01) but wrap inside a button

Instead of hacking opacity directly, you control the date picker manually.
You can create a .sheet or .popover when the user taps the text.

Example:

struct ContentViewA: View {
    @StateObject var viewModel: SampleViewModel = .init()
    @State private var isShowingDatePicker = false

    var body: some View {
        VStack {
            HStack {
                Spacer()
                Button(action: {
                    isShowingDatePicker.toggle()
                }) {
                    Text(viewModel.displayDate)
                        .font(.body)
                        .foregroundStyle(Color.blue)
                }
                .sheet(isPresented: $isShowingDatePicker) {
                    VStack {
                        DatePicker("Select a date", selection: $viewModel.selectedDate, displayedComponents: .date)
                            .datePickerStyle(.graphical)
                            .labelsHidden()
                            .padding()
                        Button("Done") {
                            isShowingDatePicker = false
                        }
                        .padding()
                    }
                    .presentationDetents([.medium])
                }
                Spacer()
            }
            .padding(.horizontal, 20)
            .padding(.top, 25)
        }
        .onAppear {
            updateDisplayDate()
        }
        .onChange(of: viewModel.selectedDate) { _ in
            updateDisplayDate()
        }
    }

    private func updateDisplayDate() {
        let formatter = DateFormatter()
        formatter.dateFormat = "HH:mm E, d MMM y"
        viewModel.displayDate = formatter.string(from: viewModel.selectedDate)
    }
}

Why is this better?

If you really must have the "inline tap to open" style (without sheet)...

You could try forcing a .datePickerStyle(.compact) and no opacity tricks, but still wrap it in an invisible Button to manage focus.

However, using .sheet is much closer to the intended modern UX.

Reasons:
  • RegEx Blacklisted phrase (0.5): Why is this
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Starts with a question (0.5): What
  • Low reputation (0.5):
Posted by: Ebillson GRAND JEAN