WheelDatePicker 在 iOS 15.0 中不可见

WheelDatePicker is invisible in iOS 15.0

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

奇怪的是,DatePicker 在与 iOS 14.4 相同的设备上运行。我认为这是一个 iOS 15 beta 错误,但是,在互联网上,我找不到关于这个错误的任何信息,这就是为什么我认为我可能做错了什么。我唯一知道的是 WheelDatePicker 在 iOS 15.

中发生了轻微的变化

该错误适用于较小的设备,即 iPod touch、iPhone SE 2020 等 - 具有 TouchID 的设备。在 iPhone SE(第 2 代)IOS 15.0(测试版 3)上,也会发生此错误。

下面附上问题的代码和截图。提前感谢您提供提示、解决方案和解释为什么会出现此问题。

代码:

// 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){
...

我也有同样的问题。

对于轮式拾取器,您只需明确设置拾取器样式.pickerStyle(.wheel),拾取器将像以前一样工作。

我遇到了同样的问题。

日期选择器是不可见的,因为我在黑暗模式下有浅色背景。所以日期选择器选择浅色字体。

只需添加

overrideUserInterfaceStyle = .light

在我的视图控制器中,日期选择器正常显示。

或者将 Appearance 键添加到您的 info.plist 中,值为 Light 以将您的整个应用程序设置为亮模式。