swift 白色矩形显示在前面的文字我写

uqdfh47h  于 2023-05-16  发布在  Swift
关注(0)|答案(1)|浏览(117)

我在做一个旅行日记应用。当我写下我所访问的城市的详细信息时,我会在城市名称下面看到一个白色的矩形,而不是我所写的。我想看看我在名字下面写了什么。
这就是我写的例子:

这是我不想看到的结果:

这是我现在的代码。有人能帮我吗

struct LandingView: View {
    @State private var oldLandingName: String = ""
    @State private var newLandingName: String = ""
    @State private var isAddingLanding = false
    @State private var landings: [String] = []
    @State private var isEditing = false
    @State private var landingDetails: [String: [String]] = [:]
    @State private var newLandingDetails: String = ""

    var body: some View {
        NavigationView {
            ZStack {
                Color.blue.ignoresSafeArea()
                Circle()
                    .scale(1.7)
                    .foregroundColor(.yellow.opacity(0.60))
                    .padding()
                Circle()
                    .scale(1.3)
                    .foregroundColor(.blue.opacity(0.50))
                    .padding()
                VStack {
                    HStack {
                        Text("Add Your Travels")
                            .bold()
                        Spacer()
                        Button(action: {
                            isAddingLanding = true
                        }) {
                            Image(systemName: "plus")
                                .resizable()
                                .frame(width: 20, height: 20)
                                .foregroundColor(.white)
                        }
                    }
                    .padding(.horizontal)

                    List(landings, id: \.self) { landing in
                        Text(landing).bold()
                            .foregroundColor(.white)
                            .listRowBackground(Color.clear)
                            .listRowSeparatorTint(.blue)
                            .onTapGesture {
                                self.isEditing = true
                                self.oldLandingName = landing
                                self.newLandingName = landing
                            }
                        if let details = landingDetails[landing] {
                            ForEach(details, id: \.self) { detail in
                                Text(detail)
                                    .foregroundColor(.white)
                            }
                        }
                    }
                    .listStyle(.plain)
                }
            }
            .navigationBarTitle(Text("Landings!"))

            .fullScreenCover(isPresented: $isAddingLanding, onDismiss: {}) {
                VStack {
                    TextField("Where did you go? \(newLandingName)", text: $newLandingName)
                        .textFieldStyle(RoundedBorderTextFieldStyle())
                        .padding()
                    TextField("Details", text: $newLandingDetails)
                        .textFieldStyle(RoundedBorderTextFieldStyle())
                        .padding()
                    Button("Add") {
                        if !newLandingName.isEmpty {
                            landings.append(newLandingName)
                            // newLandingName = ""
                            if !landingDetails.keys.contains(newLandingName) {
                                landingDetails[newLandingName] = []
                            }
                            if let details = landingDetails[newLandingName], !newLandingDetails.isEmpty {
                                landingDetails[newLandingName]?.append(newLandingDetails)
                                // newLandingDetails = ""
                            }
                            newLandingName = ""
                            newLandingDetails = ""
                        }
                        isAddingLanding = false
                    }
                    Color.blue.ignoresSafeArea()
                }
            }
            .fullScreenCover(isPresented: $isEditing, onDismiss: {}) {
                VStack {
                    TextField("", text: $newLandingName)
                        .textFieldStyle(RoundedBorderTextFieldStyle())
                        .padding()

                    Button("Add Photos") {}
                    Spacer()
                    Button("Write about it") {
                        if !newLandingName.isEmpty {
                            if let idx = landings.firstIndex(where: { $0 == $oldLandingName.wrappedValue }) {
                                self.landings[idx] = newLandingName
                                if !landingDetails.keys.contains(newLandingName) {
                                    landingDetails[newLandingName] = []
                                }
                            }
                        }
                        isEditing = false
                    }
                    Color.blue.ignoresSafeArea()
                }
                // .padding()
            }
        }
    }
}
6g8kf2rb

6g8kf2rb1#

我认为你应该先调试你的代码。所以你引用的那个,是一个空白白色格,它不是空白的,它有一个详细的值,你在第二个循环中添加到代码中。如果从代码中删除这些行。应该可以的

if let details = landingDetails[landing] {
    ForEach(details, id: \.self) { detail in
        Text(detail)
            .foregroundColor(.white)
    }
}

相关问题