HomeiOS Developmentswift - DatePicker not opening in iOS 18

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 don’t know what’s damaged. Anybody confronted/dealing with any points with date picker in iOS 18 or can counsel a workaround for this?

import SwiftUI

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

struct ContentViewA: View {
    @StateObject var viewModel: SampleViewModel = .init()
    var physique: some View {
        VStack {
            HStack(alignment: .middle) {
                Spacer()
                Textual content(viewModel.displayDate).font(.physique).foregroundStyle(Colour.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 under this textual content akin to segmented management and checklist.
        }
        .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 to be.

enter image description here

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

enter image description here

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments