【问题标题】:Xcode SwiftUI leading navigationBarItem not working but trailing navigationBarItem does workXcode SwiftUI 前导 navigationBarItem 不工作,但尾随 navigationBarItem 工作
【发布时间】:2021-08-20 13:04:35
【问题描述】:

我是一名新的 Xcode/SwiftUI 程序员,我在我的 ThirdView 代码(如下)中遇到了一个问题,即完全相同的按钮代码作为尾随的 navigationBarItem 按预期工作,但在我移动时根本没有出现按钮和相关代码到前导栏项目位置。下面的代码说明了这个问题。

此代码示例与我之前发布的问题的代码几乎相同(一位非常有帮助的 StackOverflow 社区成员帮助我解决了该问题);我的问题涉及同一代码中的不同问题。

struct ContentView: View {
    @State private var activeLink: Bool = false
    var body: some View {
        NavigationView {
            VStack {
                Spacer()
                NavigationLink("Show Second Screen",
                    destination: SecondView(active: $activeLink), isActive: $activeLink)
                Spacer()
            }.padding()
            .navigationBarTitle("First view")
        } // End of NavigationView
    } // End of body
} // End of ContentView

struct SecondView: View {
    @Binding var active: Bool
    @State private var thirdViewLink: Bool = false
    
    var body: some View {
        VStack {
        Spacer()
        NavigationLink(destination: ThirdView(thirdViewActive: $thirdViewLink),
                       isActive: $thirdViewLink) {
            EmptyView()
        } // End of NavigationLink
        Spacer()
        }.padding() // End of VStack
        .navigationBarTitle("Second View")
        .navigationBarBackButtonHidden(true)
        .navigationBarItems(leading: Button("Back to 1st View") {
            self.active = false
        }, trailing: Button("Show Third View") {
            self.thirdViewLink = true
        }
        )
    } // End of body
} // End of SecondView

struct ThirdView: View {
    @Binding var thirdViewActive: Bool
    var body: some View {
        VStack(spacing: 15) {
            Text("Third View")
            Spacer()
        }.padding() // End of VStack
        .navigationBarBackButtonHidden(true)
        //The following approach does NOT work
        .navigationBarItems(leading: Button("Back to 2nd View") {
            self.thirdViewActive = false
        })
        // This DOES work - why the difference?
     /* .navigationBarItems(trailing: Button("Back to 2nd View") {
            self.thirdViewActive = false
        }) */
    } // End of body
} // End of ThirdView

为什么“返回到第二视图”的 ThirdView 按钮仅在我将其分配给尾随 navigationBarItems() 位置时才出现?在这种情况下,如何使按钮出现在前导位置?非常感谢您提供的任何见解!

【问题讨论】:

    标签: xcode swiftui navigationbaritems


    【解决方案1】:

    您的代码在 macos 12.beta、xcode 13.beta、 目标 ios 15 和 macCatalyst 12,当我使用这个时:

    struct ContentView: View {
        @State private var activeLink: Bool = false
        var body: some View {
            NavigationView {
                VStack {
                    Spacer()
                    NavigationLink("Show Second Screen",
                        destination: SecondView(active: $activeLink), isActive: $activeLink)
                    Spacer()
                }.padding()
                .navigationBarTitle("First view")
            } // End of NavigationView
            .navigationViewStyle(.stack)  // <--- here
        } // End of body
    } // End of ContentView
    

    在 macOS 12 和 iPhone 和 iPad ios15 上测试。

    【讨论】:

    • 非常感谢您提供此解决方案!当我准确输入您的代码时,我收到一个错误(“无法推断成员'堆栈'的上下文基础”),但我将您的代码稍微修改为.navigationViewStyle(StackNavigationViewStyle()),它运行良好。再次感谢!
    • 哈哈是的,“.stack”适用于ios13+,否则你必须使用“StackNavigationViewStyle()”
    猜你喜欢
    • 1970-01-01
    • 2020-05-22
    • 2021-05-14
    • 1970-01-01
    • 2021-02-10
    • 1970-01-01
    • 1970-01-01
    • 2020-11-02
    • 1970-01-01
    相关资源
    最近更新 更多