【问题标题】:Picker support for iOS14 SwiftUIiOS14 SwiftUI 的选取器支持
【发布时间】:2020-10-06 22:29:43
【问题描述】:

我的应用在 SwiftUI 和 iOS13 上运行良好,现在我正在尝试添加对 14 的支持,部分应用如下所示:

背景中的灰色不应该在那里。我试过了,在选择器内将背景设置为白色,移除一些组件,移除框架,但没有任何效果。

代码如下:

 ZStack {
        if showingNewMealTime {
            ZStack {
                VStack(alignment: .center) {
                    ZStack{
                        Color.black.opacity(0.4)
                            .edgesIgnoringSafeArea(.vertical)
                        VStack(spacing: 20) {
                            Text("Change Time")
                                .bold().padding()
                                .frame(maxWidth: .infinity)
                                .background(Color.yellow)
                                .foregroundColor(Color.white)
                            
                            HStack {
                                VStack {
                                    Picker("", selection: $pickerHour){
                                        ForEach(1..<12, id: \.self) { i in
                                            Text("\(i)").tag(i)
                                        }
                                    }
                                    .frame(width: 50, height: 120)
                                    .clipped()
                                }
                                
                                VStack {
                                    Picker("", selection: $pickerMinutes){
                                        ForEach(0..<60, id: \.self) { i in
                                            Text("\(i)").tag(i)
                                        }
                                    }
                                    .frame(width: 50, height: 120)
                                    .clipped()
                                }
                                VStack {
                                    Picker("", selection: $pickerAmOrPm[pickerAmOrPmSelection]){
                                        ForEach(pickerAmOrPm, id: \.self) { i in
                                            Text("\(i)").tag(i)
                                        }
                                    }
                                    .frame(width: 50, height: 120)
                                    .clipped()
                                }
                            }
                            
                            Spacer()
                            
                            Button(action: {
                                ...
                            }){
                                Text("Save")
                            }
                            .padding(.horizontal)
                            .padding(.bottom, 30)
                        }
                        .frame(width:300, height: 300)
                        .background(Color.white)
                        .mask(RoundedRectangle(cornerRadius: 20))
                        .shadow(radius: 20)
                    }
                }
            }
        }
    }

发生了什么变化,我必须修复什么来纠正该灰色区域?

谢谢

【问题讨论】:

    标签: swift swiftui


    【解决方案1】:

    改用原生 DatePicker。

    import SwiftUI
    
    struct DatePickerView: View {
        @State var date: Date = Date()
    
        var body: some View {
            VStack {
                DatePicker("Date", selection: $date,
                       displayedComponents: .hourAndMinute)
                    .frame(height: 50)
                Spacer()
            }
            .padding()
        }
     }
    
     struct DatePickerView_Previews: PreviewProvider {
        static var previews: some View {
            DatePickerView()
        }
     }
    

    Image showing the result of the above code

    更多信息:

    SwiftUI DatePicker - swiftcompiled.com

    Selecting dates and times with DatePicker - hackingwithswift.com

    How to create a date picker and read values from it - hackingwithswift.com

    DatePicker - Documentation

    (2020 年 10 月 11 日编辑): 编辑添加更多信息的链接。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-04
      • 1970-01-01
      • 2021-04-18
      • 2021-01-04
      • 2019-11-27
      • 2020-01-21
      相关资源
      最近更新 更多