为什么SwiftUI呈现的视图显示不正确?

时间:2020-08-30 22:18:48

标签: swiftui swiftui-navigationview xcode12beta6

我已经从Xcode 12 beta 6(12A8189n)应用模板创建了一个SwiftUI“多平台”(iOS和macOS)应用。

我的问题是我的一个视图AnotherView显示不正确。这是显示问题的GIF。请注意,AnotherView显示的导航堆栈已被推送到不存在的视图。轻按后退按钮可显示预期的屏幕,但是仅部分填充了预期的区域。

Demo

这是代码:

TestNavigationApp.swift

import SwiftUI

@main
struct TestNavigationApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

ContentView.swift

import SwiftUI

struct ContentView: View {
    
    @State private var presentingFirstView = false
    
    var body: some View {
        Button(action: { self.presentingFirstView = true }) {
            
            Text("Present First View")
        }
        .sheet(isPresented: $presentingFirstView) {

            FirstView(isPresented: $presentingFirstView)
        }
    }
}

FirstView.swift

import SwiftUI

struct FirstView: View {
    
    @Binding var isPresented: Bool

    var body: some View {
        
        NavigationView {
            EmbeddedView()
                .navigationBarTitle("First View", displayMode: .large)
        }
    }
}

EmbeddedView.swift

import SwiftUI

struct EmbeddedView: View {
    
    @State private var presentingAnotherView = false
    
    var body: some View {

        VStack {
            Text("Embedded View")
            
            Button(action: { self.presentingAnotherView = true }) {
                
                Text("Present Another View")
            }
        }
        .sheet(isPresented: $presentingAnotherView) {

            AnotherView(isPresented: $presentingAnotherView)
        }
    }
}

AnotherView.swift

import SwiftUI

struct AnotherView: View {
    
    @Binding var isPresented: Bool
    
    var body: some View {
        NavigationView {
            Text("Another View")
                .navigationBarTitle("Another View", displayMode: .large)
        }
    }
}

无论如何,不​​是很确定这里发生了什么。任何建议表示赞赏。

1 个答案:

答案 0 :(得分:1)

尝试明确使用导航视图样式

var body: some View {
    NavigationView {
        Text("Another View")
            .navigationBarTitle("Another View", displayMode: .large)
    }.navigationViewStyle(StackNavigationViewStyle())
}