SwiftUI Swift 问答之 WheelDatePicker 在 iOS 15.0 中不可见

实战问题

我正在 SwiftUI 中构建一个需要在自定义警报中使用 DatePicker 的应用程序。每次我使用带有 WheelDatePickerStyle 的 DatePicker 时,它在 IOS 15.0 中是不可见的(也在紧凑模式下 – 单击左上角的月份和年份后)。

奇怪的是,DatePicker 与 iOS 14.4 在同一台设备上工作。我认为这是一个 iOS 15 测试版错误,但是,在互联网上,我找不到有关此错误的任何信息,这就是为什么我认为我可能做错了什么。我唯一知道的是,WheelDatePicker 在 iOS 15 中略有变化。

该错误适用于较小的设备,即 iPod touch、iPhone SE 2020 等 – 具有 TouchID 的设备。在带有 IOS 15.0(beta 3)的物理 iPhone SE(第 2 代)上,也会出现此错误。

下面,我附上了问题的代码和截图。预先感谢您提供有关此问题发生原因的提示、解决方案和解释。

代码:

// Custom DatePicker Alert
import SwiftUI

struct BirthdayPicker: View {
    // Properties
    @State private var previousDate: Date = Date()
    @Binding var birthday: Date
    @Binding var isVisible: Bool
    let savingAction: (() -> ())?

    var body: some View {
        ZStack {
            // A field outside, which - if has been tapped - hides alert with DatePicker.
            Color.black.opacity(0.00001)
                .onTapGesture {
                    withAnimation { self.isVisible = false }
                }

            // Alert with DatePicker
            VStack {
                Text(LocalizedStrings.chooseBirthday)
                    .padding(.top)

                DatePicker("",
                           selection: $birthday,
                           in: ...Date(),
                           displayedComponents: [.date])
                    .datePickerStyle(WheelDatePickerStyle())
                    .labelsHidden()

                HStack {
                    // Cancel Button
                    Button(LocalizedStrings.cancel) {
                        self.birthday = previousDate
                        self.isVisible = false
                    }
                    .foregroundColor(.red)

                    Spacer()

                    // Save Button
                    Button(LocalizedStrings.save) {
                        if let savingAction = savingAction {
                            savingAction()
                        } else {
                            self.isVisible = false
                        }
                    }
                }
                .padding([.horizontal, .bottom])
            }
            .frame(width: UIScreen.main.bounds.width - 40)
            .background(Color(.systemBackground))
            .clipShape(RoundedRectangle(cornerRadius: 15))
            .contentShape(RoundedRectangle(cornerRadius: 15))
            .shadow(radius: 10)
            .onAppear { self.previousDate = birthday }
        }
    }
}

// A sample View that uses this DatePicker alert – other than in the picture below
import SwiftUI

struct WelcomeSlide2: View {
    // Properties
    @State var birthday: Date = Date()

    var body: some View {
        ZStack {
            Background()

            GeometryReader { geom in
                ScrollView(.vertical) {
                    // Title and Subtitle
                    Text(LocalizedStrings.welcomeTitle2)
                        .font(.largeTitle)
                        .fontWeight(.semibold)
                        .padding(.top)
                    Text(LocalizedStrings.welcomeSubtitle2)
                        .font(.headline)
                        .padding([.bottom, .horizontal])
                        .multilineTextAlignment(.center)

                    // Image Button
                    Button(action: { self.showingImagePicker = true }) {
                        ProfileImagePlaceholder(image: $image, inputImage: $inputImage)
                    }
                    .frame(width: 100, height: 100)
                    .shadow(radius: 10)
                    .padding(.vertical)
                    .accessibility(label: Text(LocalizedStrings.addPhotoButton))

                    VStack(alignment: .leading) {
                        // Name TextField
                        TextField(LocalizedStrings.name, text: $name)
                            .font(.title3)

                        Divider()

                        // CUSTOM DATEPICKER ALERT - BUGGED
                        // ---------------------------------------------------------
                        // Birthday DatePicker
                        HStack {
                            Text(LocalizedStrings.birthday)
                            Spacer()
                            Button(action: { withAnimation { self.showingBirthdayPicker.toggle() } }) {
                                Text(UserDataManager.shared.dateFormatter.string(from: birthday))
                            }
                        }
                        // ---------------------------------------------------------

                        Divider()

                        // Zodiac Sign
                        HStack {
                            Text(LocalizedStrings.zodiacSign)
                            Spacer()
                            Text(zodiacSign)
                        }

                        Divider()

                        // About
                        Text(LocalizedStrings.aboutMe)
                        TextEditor(text: $about)
                            .foregroundColor(.gray)
                            .offset(x: -4, y: -15)
                    }
                    .padding(.horizontal)
                    .font(.subheadline)

                    Spacer()

                    HStack {
                        Spacer()
                        NavigationLink(destination: WelcomeSlide3(), isActive: $didTapNextSlide) {
                            Button {
                                self.containsCustomImage = inputImage == nil ? false : true
                                self.zodiacSign = didChangeDate ? zodiacSign : ""
                                self.didTapNextSlide = true
                            } label: {
                                Image(systemName: "arrow.right")
                                    .font(.system(size: 30))
                                    .foregroundColor(Color(.label))
                            }

                        }
                    }
                    .padding([.trailing, .bottom, .top])
                }
                .frame(minHeight: geom.size.height)
                .overlay(Color.black.opacity(showingBirthdayPicker ? 0.5 : 0).ignoresSafeArea())

                if self.showingBirthdayPicker {
                    BirthdayPicker(birthday: $birthday, isVisible: $showingBirthdayPicker, savingAction: nil)
                }
            }
        }
        .navigationBarHidden(true)
    }
}

解决方案

将此添加到您的课程中:
看起来 DatePicker 不在屏幕上。试试这个:

// Alert with DatePicker
VStack (alignment: .leading){

…# 加入我们一起学习SwiftUI
QQ:3365059189
SwiftUI技术交流QQ群:518696470
教程网站:www.openswiftui.com

发表回复