【问题标题】:swiftUI form bug on XCode13 and iOS15 and iPadOS15XCode 13 和 iOS 15 和 iPadOS 15 上的 swiftUI 表单错误
【发布时间】:2021-11-18 02:23:01
【问题描述】:

我正在 Xcode 12 上构建一个表单并使用 iOS 14,一切都按预期工作。但是在切换到 13 和 iOS 15 后,单元格的高度不一致,甚至在滚动到屏幕外时也会发生变化,就像它们是可重复使用的表格单元格一样。

例如,我有这个代码:*** 编辑:我更改了下面的代码以包含当前在该页面上的所有内容。

    Struct EvalViewMHx: View {
    var body: some View {
    VStack {
        
        Form {
            
            Label(
                title: {
                    Text("Demographics & Medical History")
                        .font(.title)
                        .foregroundColor(.white)
                        .padding()
                },
                icon: {
                    Image(systemName: "heart.text.square.fill")
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .foregroundColor(.white)
                    
                })
                .listRowBackground(Color.blue)
                .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .center)
            
            
            Section(header: Text("Demographics")) {
                demographicsView(nameText: $nameText, DOB: $DOB, height: $height, weight: $weight)
            }
            
            
            
            Section(header: Text("Medical History")) {
                
                VStack {
                    HStack {
                        Text("When did you get hurt?")
                        
                            .padding()
                        
                        Spacer()
                        
                        Toggle("", isOn: $doesNotKnowInjuryDate)
                            .frame(width: 35)
                            .padding(.horizontal)
                        
                        Text("I don't know")
                            .fontWeight(!doesNotKnowInjuryDate ? .regular : .bold)
                            .padding(.leading)
                        
                    }
                    
                    if !doesNotKnowInjuryDate {
                        DatePicker("Injury Date", selection: $injuryDate, displayedComponents: [.date])
                            .foregroundColor(Color(UIColor.systemGray3))
                            .padding()
                    }
                    
                }
                .frame(minWidth: .zero, idealWidth: .infinity, maxWidth: .infinity, minHeight: 150, idealHeight: 150, maxHeight: 150, alignment: .center)
                
                VStack(alignment: .leading) {
                    
                    HStack {
                        Text("Where is your injury?")
                        
                        Spacer()
                        
                        Text("Right")
                            .fontWeight(isInjuryOnRightSide ? .regular : .bold)
                        
                        Toggle("", isOn: $isInjuryOnRightSide)
                            .toggleStyle(SwitchToggleStyle(tint: Color(UIColor.systemGray5)))
                            .frame(width: 35)
                            .padding(.horizontal)
                        
                        Text("Left")
                            .fontWeight(!isInjuryOnRightSide ? .regular : .bold)
                            .padding(.leading)
                    }
                    .zIndex(2.0)
                    
                    
                    
                    Picker(selection: $injuryLocation, label: Text(""), content: {
                        ForEach(injuryLocations, id: \.self) { location in
                            Text(location)
                        }
                    })
                    .pickerStyle(WheelPickerStyle())
                    .frame(minWidth: .zero, idealWidth: .infinity, maxWidth: .infinity, minHeight: 100, idealHeight: 100, maxHeight: 100, alignment: .center)
                    .zIndex(1.0)
                    .padding()
                    
                    
                    
                }
                .padding()
                
                VStack(alignment: .leading) {
                    Text("How did you get hurt? (Method of Injury)")
                        .padding()
                    
                    TextEditor(text: $MOI)
                        .frame(height: 150)
                        .background(Color(UIColor.systemGray5).opacity(0.75))
                        .cornerRadius(15)
                    
                }
                .frame(minWidth: .zero, idealWidth: .infinity, maxWidth: .infinity, minHeight: 200, idealHeight: 200, maxHeight: 200, alignment: .center)
                .padding()
                
                if injuryLocation != "Lower Back" && injuryLocation != "Upper Back" && injuryLocation != "Head / Neck" && injuryLocation != "Chest" {
                    
                    VStack {
                        pastInjuryHistory(injuryLocation: $injuryLocation, hasHadSameSideInjury: $hasHadSameSideInjury, hasHadOppositeSideInjury: $hasHadOppositeSideInjury, isSameSide: true)
                        
                        pastInjuryHistory(injuryLocation: $injuryLocation, hasHadSameSideInjury: $hasHadSameSideInjury, hasHadOppositeSideInjury: $hasHadOppositeSideInjury, isSameSide: false)
                        
                    }
                }
            }
        }
    }
}
    }

(在下图中,您还可以在顶部看到一些按钮,它们只是水平滚动视图中的矩形。然后上面的代码就放在标签视图中的下方,如下所示:)

                        TabView(selection: $activeTab) {
            
            EvalViewMHx()
                .tag(1)
            
            EvalObjectiveView()
                .tag(2)
            
            viewThree(activeTab: $activeTab)
                .tag(3)
            
            viewFour(activeTab: $activeTab)
                .tag(4)
            
        }
        .tabViewStyle(PageTabViewStyle())
        .onChange(of: activeTab, perform: { value in
            print(activeTab)
        })
        .animation(.default)

输出显示其中一个单元格的高度完全不同。

更奇怪的是,另一个部分有一个高度为 0 的单元格,但仍显示文本字段位于其余单元格之上。我有一个选择器视图,它也在这些单元格中,并将选择器样式放置到

.pickerStyle(InlinePickerStyle())

将单元格扩展到大于 iPad 屏幕尺寸,并且不像以前的版本那样将其放入轮子中。

这些错误是我应该耐心等待修复,还是我可以做些什么来修复这些错误并继续推进我的项目?

【问题讨论】:

  • 为了帮助我们追踪您的问题,您能否展示一个可重现的示例。您显示的示例代码与图片不匹配。 HStack 是在 List 还是 Form 中?
  • 我也遇到了这个问题。你能确定原因吗?
  • 不,很遗憾没有。我一直在推迟该项目的工作,希望 Apple 会发布某种修复程序,但我会在这里发布我的代码,看看是否有是我们做错了什么。

标签: ios forms swiftui ios15


【解决方案1】:

在同一个问题上苦苦挣扎。原来 .animation 导致了这个问题,因为它自 iOS 15 以来已被弃用。删除该行为我解决了这个问题。

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center。
猜你喜欢
  • 2021-12-21
  • 2021-11-10
  • 1970-01-01
  • 1970-01-01
  • 2021-12-09
  • 2021-09-26
  • 2021-10-05
  • 2021-11-08
  • 2022-01-13
相关资源
最近更新 更多