swift – DatePicker not opening in iOS 18

0
5
swift – DatePicker not opening in iOS 18


I’ve a textual content factor which shows date chosen from Date Picker. When tapped on the textual content, app opens date picker over all the opposite components within the view. This logic was working completely till iOS 17. It would not work anymore with iOS 18.

I do not know what’s damaged. Anybody confronted/going through any points with date picker in iOS 18 or can counsel a workaround for this?

import SwiftUI

class SampleViewModel: ObservableObject {
    @Revealed var selectedDate = Date.now
    @Revealed var displayDate = ""
}

struct ContentViewA: View {
    @StateObject var viewModel: SampleViewModel = .init()
    var physique: some View {
        VStack {
            HStack(alignment: .heart) {
                Spacer()
                Textual content(viewModel.displayDate).font(.physique).foregroundStyle(Coloration.blue)
                    .overlay {
                        DatePicker(choice: $viewModel.selectedDate, displayedComponents: .date) {}
                            .labelsHidden()
                            .allowsHitTesting(true)
                            .contentShape(Rectangle())
                            .opacity(0.011)
                    }
                Spacer()
            }
            .padding([.leading, .trailing], 20.0)
            .padding([.top], 25.0)
            .contentShape(Rectangle())

            // There are different components beneath this textual content reminiscent of segmented management and listing.
        }
        .onAppear {
            let formatter = DateFormatter()
            formatter.dateFormat = "HH:mm E, d MMM y"
            viewModel.displayDate = formatter.string(from: viewModel.selectedDate)
        }
        .onChange(of: viewModel.selectedDate) { newValue in
            let formatter = DateFormatter()
            formatter.dateFormat = "HH:mm E, d MMM y"
            viewModel.displayDate = formatter.string(from: newValue)
        }
    }
}

Edit: Commenting out allowsHitTesting, contentShape and opacity modifiers exposes the date picker with it is default type and that is how the UI seems.

enter image description here

What I need to show is the picked date on a Textual content view factor with out displaying the date picker styling, one thing like this.

enter image description here

LEAVE A REPLY

Please enter your comment!
Please enter your name here