ios 为什么SwiftUI会出现这种情况?

hfwmuf9z  于 2023-08-08  发布在  iOS
关注(0)|答案(2)|浏览(82)

我有一个下拉菜单,行为有点奇怪,见here
文本是一步一步地更新的,而不是瞬间的,更新后会有一点颤抖。

相关代码如下:
使用的状态变量

@State private var gender = ""
@State private var birthDate = Date.now
@State private var formattedDate = ""
@State private var genderMenuTitle = "Gender"

字符串

下拉菜单所在的Form:

Form {
    Section(header: Text("Gender")) {
        Menu(genderMenuTitle) {
            Button (action: {
                gender = "F"
                genderMenuTitle = "Female"
                print(gender)
            }, label: {
                Text("Female")
            })
            Button (action: {
                gender = "M"
                genderMenuTitle = "Male"
                print(gender)
            }, label: {
                Text("Male")
            })
            Button (action: {
                gender = "O"
                genderMenuTitle = "Other"
                print(gender)
            }, label: {
                Text("Other")
            })
            Button (action: {
                gender = "?"
                genderMenuTitle = "Rather not say"
                print(gender)
            }, label: {
                Text("Rather not say")
            })
      }
}


为什么会这样?

tvz2xvvm

tvz2xvvm1#

因为每次用户单击按钮时都要手动更新视图,所以可以使用withAnimation闭包使这个过程更加流畅。

withAnimation {

gender = "F"
genderMenuTitle = "Female"

}

字符串
但我会建议你只使用拾取器代替。

struct ContentView: View {
    
    @State private var gender = "Male"
    
    var genderOptions = [
        "Female",
        "Male",
        "Other",
        "Rather not say"
    ]
    
    var body: some View {
        VStack {
            Form {
                Section(header: Text("Gender")) {
                    Picker("", selection: $gender) {
                        ForEach(genderOptions, id: \.self) { key in
                            Text(key)
                                .tag(key)
                        }
                    }
                    .labelsHidden()
                    .pickerStyle(.menu)
                }
            }
        }
    }
}

tcomlyy6

tcomlyy62#

这并不是特别针对你,但是如果你有一个Menu,你在其中改变了标题,这是典型的,不要使用初始化器init(_ titleKey: LocalizedStringKey, @ViewBuilder content: () -> Content) where Label == Text,而是像下面这样使用init(@ViewBuilder content: () -> Content, @ViewBuilder label: () -> Label)。我还通过使用enum来减少状态跟踪的数量来简化事情:

struct WeirdMenuView: View {
    
    @State private var gender: Gender? = nil

    var body: some View {
        Form {
            Section(header: Text("Gender")) {
                Menu {
                    Button (action: {
                        gender = .female
                        print(gender ?? "")
                    }, label: {
                        Text("Female")
                    })
                    Button (action: {
                        gender = .male
                        print(gender ?? "")
                    }, label: {
                        Text("Male")
                    })
                    Button (action: {
                        gender = .other
                        print(gender ?? "")
                    }, label: {
                        Text("Other")
                    })
                    Button {
                        gender = .ratherNotSay
                        print(gender ?? "")
                    } label: {
                        Text("Rather not say")
                    }

                } label: {
                    // Use an actual view here. *****
                    HStack {
                        Text(gender?.rawValue.capitalized ?? "Gender")
                        Spacer()
                    }
                }

            }
        }
    }
}

enum Gender: String {
    case female = "Female"
    case male = "Male"
    case other = "Other"
    case ratherNotSay = "Rather not say"
}

字符串
编辑:
这正是我看到我的代码所做的:


的数据

相关问题