forms - 带有动画的 SwiftUI 表单布局错误
问题描述
我已经阅读了关于这个问题的类似描述,但没有一个是完全正确的,而且解决方法似乎不适用于我。我想知道是否有其他人经历过这种情况,是否有办法使用动画来显示表单,或者这是否值得将其作为错误报告给 Apple。
我有ContentView
几个按钮可以切换布尔值。一个按钮简单地切换布尔值,而另一个按钮在动画块内切换布尔值。布尔值控制显示两个表单视图中的哪一个。在没有动画的情况下切换时,表单会正确呈现。但是当在动画块中切换时,表单呈现不正确。
这是“正常”(又名ReviewSheet
)视图。
当您点击“编辑”时,它会切换到BugSheet
视图。看起来不错。
如果您选择“Bug It”按钮,这BugSheet
就是呈现方式:
现在,表单中BugSheet
的第一部分header
与放大的内容区域重叠,在第二部分中,Text
不再呈现多行(使用lineLimit
修饰符无效)。
以下是这三个视图中的每一个的代码:
struct ContentView: View {
@State private var isEditing = false
var body: some View {
VStack(alignment: .leading, spacing: 0) {
HStack {
Text("Form Bug")
.font(.title)
Spacer()
// This "Edit" button just toggles isEditing and doing this
// the switch between BugSheet and ReviewSheet appears as you
// would expect.
Button(isEditing ? "Save 1" : "Edit") {
isEditing.toggle()
}
// This "Edit" button also toggle isEditing but does so within
// an animation block. The switch between BugSheet and ReviewSheet
// leaves the Form rendered improperly.
Button(isEditing ? "Save 2" :
"Bug It") {
withAnimation {
isEditing.toggle()
}
}
}
.padding()
.background(Color.orange)
.foregroundColor(.white)
if isEditing {
BugSheet()
} else {
ReviewSheet()
}
}
}
}
struct ReviewSheet: View {
@State private var titleText: String = "Example Title"
@State private var shortDescription: String = "This should work"
@State private var keywords: String = "swiftui, apple, form, bug"
let longDescription = "The rain in Spain stays mainly on the Plains even while the quick brown fox jumps over the lazy dog."
var body: some View {
Form {
Section(header: Text("Title")) {
Text(titleText)
}
Section(header: Text("Details")) {
Text(shortDescription)
Text(longDescription)
Text(keywords)
}
}
}
}
struct BugSheet: View {
enum Field: Hashable {
case title
case shorty
case keywords
}
@FocusState private var focusedField: Field?
@State private var titleText: String = ""
@State private var shortDescription: String = ""
@State private var keywords: String = ""
@State private var notes: String = "This is a starter note."
let longDescription = "The rain in Spain stays mainly on the Plains even while the quick brown fox jumps over the lazy dog."
private func gotoField(_ field: Field) {
focusedField = field
}
var body: some View {
Form {
Section(header: Text("Title")) {
TextField("title here", text: $titleText, onCommit: {gotoField(.shorty)})
.autocapitalization(.words)
.disableAutocorrection(false)
.keyboardType(.default)
.focused($focusedField, equals: .title)
}
Section(header: Text("Details")) {
TextField("short description", text: $shortDescription, onCommit: {gotoField(.keywords)})
.autocapitalization(.sentences)
.disableAutocorrection(false)
.keyboardType(.default)
.focused($focusedField, equals: .shorty)
Text(longDescription)
TextField("keywords", text: $keywords, onCommit: {gotoField(.title)})
.autocapitalization(.none)
.disableAutocorrection(true)
.keyboardType(.default)
.focused($focusedField, equals: .keywords)
}
}
}
}
解决方案
推荐阅读
- gatsby - 为什么自定义 404 页面在 gatsby 应用程序中重复
- three.js - object.updateWorldMatrix 不是函数
- javascript - 我可以部署一个 Vue Cli 应用程序作为另一个项目的依赖项吗?
- ag-grid - 从 ag-grid 中的工具面板检测拖动列上的事件
- javascript - 带有 javascript 的 SOAP Web 服务客户端
- pyspark - 使用 spark sql 进行条件滞后求和
- javascript - 在输入字段上复制/粘贴验证并限制字符
- html - 如何使用对象拟合将图像叠加在另一个图像的角落
- mule - 执行 mule 批处理流时出现异常,例如无法通过提及文件的路径来删除对象存储中的文件
- axapta - 将 doc 文件保存为 word 模板