【问题标题】:WheelDatePicker is invisible in iOS 15.0WheelDatePicker 在 iOS 15.0 中不可见
【发布时间】:2021-10-01 17:27:17
【问题描述】:

我正在 SwiftUI 中构建一个需要在自定义警报中使用 DatePicker 的应用程序。每次我将 DatePicker 与 WheelDatePickerStyle 一起使用时,它在 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)
    }
}

屏幕截图

【问题讨论】:

    标签: ios swift iphone swiftui datepicker


    【解决方案1】:

    我也有同样的问题。

    对于滚轮选择器,您只需要显式设置选择器样式.pickerStyle(.wheel),选择器就会像以前一样工作。

    【讨论】:

    • 这为我修好了,谢谢!
    【解决方案2】:

    看起来 DatePicker 不在屏幕上。试试这个:

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

    【讨论】:

    • 不幸的是,它不起作用。 Currently, it looks like this. 有趣的是,这个视图被剪掉了(正如你在上面的截图中看到的那样)——所以我想我会删除这行代码:.frame(width: UIScreen.main.bounds.width - 40) 这产生了一个有趣的效果——DatePicker 看起来很适合屏幕,但它仍然不显示。 Here's a second screenshot.
    猜你喜欢
    • 2021-11-17
    • 1970-01-01
    • 2021-09-20
    • 1970-01-01
    • 2021-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多