代码: 我只想在选定的按钮视图上勾选,并且需要选定的按钮schoolCode,但是我的代码也得到了复选标记和选定的按钮代码,但是如果我选择按钮,它的位置就会改变..如果我选择第三个按钮,那么它就会像那样进入第二位...请修复这个错误
ScrollView(.horizontal, showsIndicators: false) {
HStack {
if let retrievedArray = UserDefaults.standard.stringArray(forKey: "SavedSchoolCodes") {
let uniqueItems = Array(Set(retrievedArray))
ForEach(uniqueItems, id: \.self) { item in
if let school = schoolList.first(where: { $0.schoolCode == item }) {
Button {
viewModel.schoolCode = school.schoolCode ?? ""
} label: {
ZStack {
let correctedPhotoURL = school.logo?.replacingOccurrences(of: "//SchImg", with: "/SchImg") ?? ""
VStack {
URLImageView(url: correctedPhotoURL, placeholder: "NoProfilePic", width: 50, height: 50)
.clipShape(Circle())
.padding(.bottom, 4)
Text(school.schoolCode ?? "")
.font(.headline)
.foregroundColor(.black)
}
.frame(width: 100, height: 120)
.background(Color.gray.opacity(0.2))
.cornerRadius(10)
RoundedRectangle(cornerRadius: 10)
.stroke(viewModel.schoolCode == school.schoolCode ? Color.green : Color.clear, lineWidth: 2)
.frame(width: 100, height: 120)
if viewModel.schoolCode == school.schoolCode {
Image(systemName: "checkmark.circle.fill")
.resizable()
.frame(width: 20, height: 20)
.foregroundColor(.green)
.offset(x: 35, y: -45)
}
}
}
.padding(5)
} else {
Text("\(item),")
}
}
} else {
Text("No array found in UserDefaults.")
}
}
.padding(.horizontal)
}
o/p:当我选择时,我只想要复选标记。请指导如何修复更改位置。