首页 > 解决方案 > 标题重复,并且按钮在不完整的向后滑动时不起作用

问题描述

以制作代码为例。如果您对上一个视图进行不完整的滑动,那么在滚动的情况下,编辑器的文本开始与标题重复。即使没有@Environment,我也检查了它,但是通过传输的isActive,但仍然是这个错误。此外,如果滑动不完整,按钮将停止工作。有人可以知道完整的 NavigationView 教程以避免一些错误吗?我需要标题不要跳,按钮很好。

struct View1: View {

@Environment(\.presentationMode) var presentationMode
    

    var body: some View {
        NavigationView {
            ZStack {
                Color.red
                NavigationLink("View 2", destination: View2())
            }
            .navigationBarTitle("Back").navigationBarHidden(true).animation(.default)
        }
    }
}

struct View2: View {
    @State private var texti: String = ""
    
    @Environment(\.presentationMode) var presentationMode
    
    
    var body: some View {
        ZStack {
            VStack {
                TextEditor(text: $texti)
                    .padding().onReceive(texti.publisher.collect()) {
                        self.texti = String($0.prefix(2000))}
            }
        }
        .navigationBarTitle("Your Title")
        .navigationBarItems(trailing: HStack { Button(action:{}) {
            Text("Ed").foregroundColor(Color(#colorLiteral(red: 0.3647058824, green: 0.6901960784, blue: 0.4588235294, alpha: 1)))
        }; Button(action: {}) {
            Text("S").foregroundColor(Color(#colorLiteral(red: 0.3647058824, green: 0.6901960784, blue: 0.4588235294, alpha: 1)))
        }})
    }
}

标签: swiftswiftui

解决方案


推荐阅读