从 NavigationView 呈现工作表会弹出视图

Posted

技术标签:

【中文标题】从 NavigationView 呈现工作表会弹出视图【英文标题】:Presenting a sheet pops views from NavigationView 【发布时间】:2020-08-13 16:24:42 【问题描述】:

我有一个包含 NavigationView 的视图 A。 A 通过激活 NavigationLink 来推送视图 B。 B 通过激活 NavigationLink 推送视图 C。 C 通过点击 NavigationLink 来推送视图 D。 D 然后将视图呈现为工作表。如果我关闭工作表,当前的顶视图是 B。出于某种原因,显示工作表会弹出 2 个屏幕。

有人可以解释这种行为以及如何解决它吗? 我猜想视图 B(推送视图 C)中的导航链接以某种方式被停用,但我不知道为什么

从我测试的结果来看,当工作表出现时,ViewB 会重新初始化,这将重新初始化我的虚拟机,从而停用链接。如何阻止视图重新初始化?

struct ViewA: View 
@SwiftUI.Environment(\.presentationMode) private var presentationMode: Binding<PresentationMode>
@ObservedObject private var viewModel: ViewAVM

var body: some View 
    NavigationView 
        GeometryReader  fullView in
            ScrollView(.vertical, showsIndicators: false) 
                if !self.viewModel.loading 
                    VStack(alignment: .leading, spacing: 0) 
                        DetailsView(car: self.$viewModel.data)
                        Spacer(minLength: 20)
                        NavigationLink(destination: ViewB(rootActive: self.$viewModel.showB), isActive: self.$viewModel.showB) 
                            Button(action: 
                                self.viewModel.showB = true
                            ) 
                                SecondaryButtonView(enabled: true, title: "Go")
                            
                        .isDetailLink(false).padding([.leading, .trailing], 20)
                    .frame(minHeight: fullView.size.height)
                
            
        
        .navigationBarBackButtonHidden(true)
        .navigationBarTitle("View A")
        .navigationBarItems(leading: Button(action: 
            self.presentationMode.wrappedValue.dismiss()
        ) 
            Image("LeftArrow").renderingMode(.original)
        )
        .onAppear 
            self.viewModel.update()
        
    
    .navigationBarTitle("")
    .navigationBarHidden(true)

struct ViewBUI: View 
@SwiftUI.Environment(\.presentationMode) private var presentationMode: Binding<PresentationMode>
@ObservedObject private var viewModel: ViewBVM
private let rootActive: Binding<Bool>
var body: some View 
    GeometryReader  fullView in
        ScrollView(.vertical, showsIndicators: false) 
            VStack(alignment: .leading, spacing: 0) 
                Text("").font(.regularParagraph).padding(.top, 15).padding(.bottom, 10)
                CustomEntryView()
                Spacer(minLength: 20)
                NavigationLink(destination: ViewC(rootActive: self.rootActive), isActive: self.$viewModel.showC) 
                    Button(action: 
                        self.hideKeyboard()
                        self.viewModel.validate()
                    ) 
                        PrimaryButtonView(enabled: self.viewModel.valid, title: "Continue")
                    .disabled(!self.viewModel.valid)
                .isDetailLink(false).padding(.bottom, 16)
            .padding([.leading, .trailing], 20).frame(minHeight: fullView.size.height)
        
    
    .navigationBarBackButtonHidden(true)
    .navigationBarTitle(viewModel.new ? "New" : "Old")
    .navigationBarItems(leading: Button(action: 
        self.viewModel.rootActive.wrappedValue = false
    ) 
        Image("LeftArrow").renderingMode(.original)
    )

struct ViewDUI: View 
@SwiftUI.Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
let isDriver: Bool
let car: Binding<Car?>
let rootActive: Binding<Bool>

var body: some View 
    GeometryReader  fullView in
        ScrollView(.vertical, showsIndicators: false) 
                VStack(alignment: .leading, spacing: 0) 
                    Text("").font(.regularParagraph).foregroundColor(Color.black).padding([.leading, .trailing], 20).padding(.top, 15)
                    CarDetailsView(car: self.car)
                    Spacer(minLength: 20)
                    if self.car.wrappedValue?.mot == true && self.car.wrappedValue?.taxed == true 
                        if self.isDriver 
                            Button(action: 
                                self.rootActive.wrappedValue = false
                            ) 
                                PrimaryButtonView(enabled: true, title: "Save")
                            .padding(.bottom, 16).padding([.leading, .trailing], 20)
                         else 
                            NavigationLink(destination: ViewE(rootActive: self.$viewModel.showB)) 
                                PrimaryButtonView(enabled: true, title: "Continue")
                            .isDetailLink(false).padding(.bottom, 16).padding([.leading, .trailing], 20)
                        
                     else 
                        Button(action: 
                            self.presentationMode.wrappedValue.dismiss()
                        ) 
                            PrimaryButtonView(enabled: true, title: "")
                        .padding(.bottom, 16).padding([.leading, .trailing], 20)
                    
                .frame(minHeight: fullView.size.height)
        
    
    .navigationBarBackButtonHidden(true)
    .navigationBarTitle("")
    .navigationBarItems(leading: Button(action: 
        self.presentationMode.wrappedValue.dismiss()
    ) 
        Image("LeftArrow").renderingMode(.original)
    )

struct ViewEUI: View 
@SwiftUI.Environment(\.presentationMode) private var presentationMode: Binding<PresentationMode>
private let rootActive: Binding<Bool>
private let isDriver: Bool
@State private var showingScanner = false
@State private var scanHandler: LicenseScanHandler!
@State private var scanCompleted = false

var body: some View 
    VStack(alignment: .leading, spacing: 0) 
        Text("")
            .font(.regularParagraph)
            .padding(.top, 15)
            .padding([.leading, .trailing], 20)
        ZStack(alignment: .topLeading) 
            VStack(alignment: .leading, spacing: 0) 
                Image("sample")
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .padding(20)
            
            .overlay(RoundedRectangle(cornerRadius: 3).stroke(Color.azure, lineWidth: 1))
            .background(Color.white.edgesIgnoringSafeArea(.all))
            .padding(.top, 12)
            Text("")
            .font(.tinyParagraph)
            .foregroundColor(.azure)
            .frame(width: 80, height: 24)
            .background(Color.white.edgesIgnoringSafeArea(.all))
            .cornerRadius(8)
            .overlay(RoundedRectangle(cornerRadius: 8).stroke(Color.azure, lineWidth: 1))
            .padding(.leading, 20)
        
        .padding([.leading, .trailing], 40)
        .padding(.top, 16)
        Spacer(minLength: 20)
        Button(action: 
            self.showingScanner = true
        ) 
            PrimaryButtonView(enabled: true, title: "Scan")
        .padding(.bottom, 16).padding([.leading, .trailing], 20)
        .sheet(isPresented: $showingScanner) 
            ScannerWrapper(handler: self.scanHandler)
        
        NavigationLink(destination: InfoUI(), isActive: $scanCompleted) 
            Text("")
        .isDetailLink(false)
    
    .background(Color.offWhite.edgesIgnoringSafeArea(.all))
    .navigationBarBackButtonHidden(true)
    .navigationBarTitle("")
    .navigationBarItems(leading: Button(action: 
        self.presentationMode.wrappedValue.dismiss()
    ) 
        Image("LeftArrow").renderingMode(.original)
    )
    .onAppear 
        self.scanHandler = LicenseScanHandler(delegate: self)
    

【问题讨论】:

你能展示你的代码吗? @Asperi 添加了代码示例 【参考方案1】:

虽然没有记录,但我意识到在通过.isDetailLink(false) 将视图本身设置为“无详细视图”时呈现.sheet() 会导致此问题。

我明白为什么,好像视图不是详细视图,它不能显示工作表,因此工作表实际上由第一个详细视图呈现,这就是为什么它在显示工作表后立即返回该视图.

如果您确实需要它不是详细信息链接,请尝试删除 .isDetailLink(false) 并找到其他解决方法。

【讨论】:

以上是关于从 NavigationView 呈现工作表会弹出视图的主要内容,如果未能解决你的问题,请参考以下文章

从 UIButton 中获取 UIButton 的标签,该 UIButton 位于此 UIButton 呈现的弹出框中

为啥PXCOOK一切图,PSCC2017就会弹出出现一个问题,停止工作?

SwiftUI 弹出框在 NavigationView 中时消失

如何将 NavigationView 从具有固定尺寸的 TabView 扩展到全屏?

使用具有水平紧凑 UITraitCollection 的自定义 UIPresentationController,否则会弹出一个

SwiftUI中sheet弹出嵌在NavigationView中的子视图无法用presentationMode关闭(dismiss)弹出视图的解决