我正在SwiftUI中构建一个应用程序,它需要自定义警报中的DatePicker。每次我将DatePicker与WheelDatePickerStyle一起使用时,它在IOS 15.0中是不可见的(也是在紧凑模式下--在左上角单击一个月和一年后)。
奇怪的是,DatePicker与iOS 14.4在同一个设备上工作。我以为这是iOS 15测试版的错误,但是,在互联网上,我找不到关于这个错误的任何信息,这就是为什么我认为我可能做错了什么。我唯一知道的是,WheelDatePicker在iOS 15中略有变化。
这一错误适用于较小的设备,如iPod触摸、iPhone SE 2020等--那些带有TouchID的设备。在具有IOS15.0 (beta 3)的物理iPhone SE (第二代)上,也会发生此错误。
下面,我附上了问题的代码和屏幕截图。提前感谢您的提示,解决方案,并解释为什么会出现这个问题。
码
// 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)
}
}
截图:
发布于 2021-09-23 09:09:32
我也有同样的问题。
对于轮式选择器,您只需要显式地设置一个选择器样式的.pickerStyle(.wheel)
,然后选择器就会像以前一样工作。
发布于 2021-07-25 13:21:57
看上去DatePicker已经离开屏幕了。试试这个:
// Alert with DatePicker
VStack (alignment: .leading){
...
发布于 2022-05-09 18:27:13
我也遇到过同样的问题。
日期选择器是无形的,因为我有一个黑暗模式的光背景。因此,日期选择器选择一个浅色字体颜色。
只需加上
overrideUserInterfaceStyle = .light
在我看来,控制器和日期选择器看起来与正常情况一样。
或者,将Appearance
键添加到您的info.plist和值Light
中,将整个应用程序设置为轻量级模式。
https://stackoverflow.com/questions/68518594
复制相似问题