禁用 SwiftUI NavigationView 滑动到弹出功能

发布于 2025-01-21 00:18:33 字数 1927 浏览 2 评论 0原文

我正在使用Swiftui NavigationView导航到三页。 导航工作正常,我可以转到所需的页面,然后使用后面按钮返回上一页。

我的问题是,每当我从屏幕的最左侧部分滑动时,它都会弹出当前的屏幕。

每当我从屏幕的最左侧部分拖动时,我当前的屏幕也会沿着显示堆栈的上一个屏幕移动,然后弹出上述当前屏幕。

有没有办法禁用此功能?

第2页到一

import SwiftUI

struct ContentView: View {
    @State var destinationKey: String? = nil;

    var body: some View {
        NavigationView{
            VStack{
                Text("Hello, world! Landing Page")
                    .padding()
            
                Button(action: {
                    destinationKey = "pageOne"
                }) {
                    Text("Go To Page One")
                        .padding()
                }
            
            }
            .background(
                NavigationLink(destination: PageOne(), tag: ("pageOne"), selection: $destinationKey){
                    EmptyView()
                }
            )
        }
        .navigationViewStyle(StackNavigationViewStyle())

    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

import SwiftUI

struct PageOne: View {
    
    var body: some View {
        VStack{
            Text("Hello, World! Page One")
        
            NavigationLink(destination: PageTwo(), label: {
                Text("Go To Page Two")
                    .padding()
            
            })
        
        
        }
    }
}

struct PageOne_Previews: PreviewProvider {
    static var previews: some View {
        PageOne()
    }
}

import SwiftUI

struct PageTwo: View {
    var body: some View {
        Text("Hello, World! Page Two")
    }
}

struct PageTwo_Previews: PreviewProvider {
    static var previews: some View {
        PageTwo()
    }
}

I am using SwiftUI NavigationView to navigate to three pages.
Navigation works fine, I can go to my desired pages and go back to the previous page using the back button.

My problem is whenever i swipe from the leftmost part of the screen, it pops my current screen.

Whenever I drag from the leftmost part of the screen, my current screen also moves along showing the previous screen from the stack then pops the said current screen.

Is there a way to disable this feature?

Page Two to One

Page One to Landing

ContentView.swift

import SwiftUI

struct ContentView: View {
    @State var destinationKey: String? = nil;

    var body: some View {
        NavigationView{
            VStack{
                Text("Hello, world! Landing Page")
                    .padding()
            
                Button(action: {
                    destinationKey = "pageOne"
                }) {
                    Text("Go To Page One")
                        .padding()
                }
            
            }
            .background(
                NavigationLink(destination: PageOne(), tag: ("pageOne"), selection: $destinationKey){
                    EmptyView()
                }
            )
        }
        .navigationViewStyle(StackNavigationViewStyle())

    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

PageOne.swift

import SwiftUI

struct PageOne: View {
    
    var body: some View {
        VStack{
            Text("Hello, World! Page One")
        
            NavigationLink(destination: PageTwo(), label: {
                Text("Go To Page Two")
                    .padding()
            
            })
        
        
        }
    }
}

struct PageOne_Previews: PreviewProvider {
    static var previews: some View {
        PageOne()
    }
}

PageTwo.swift

import SwiftUI

struct PageTwo: View {
    var body: some View {
        Text("Hello, World! Page Two")
    }
}

struct PageTwo_Previews: PreviewProvider {
    static var previews: some View {
        PageTwo()
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千寻… 2025-01-28 00:18:33

截至目前,禁用滑动关闭的唯一方法是禁用后退按钮:

 .navigationBarBackButtonHidden(true)

如果您希望仍然有后退按钮,您可以创建一个自定义按钮:

.navigationBarItems(leading: Button("Back"){self.presentationMode.wrappedValue.dismiss()})

注意:此代码放置在呈现视图中并记住 执行的操作

@Environment(\.presentationMode) var presentationMode

在 iOS 14 及之前的结构中 ,
或者,如果您使用的是 iOS 15,您可以简单地执行以下操作:

@Environment(\.dismiss) var dismiss

并在自定义后退按钮中调用miss():

.navigationBarItems(leading: Button("Back"){dismiss()})

As of now, the only way to disable the swipe to dismiss is to disable the back button:

 .navigationBarBackButtonHidden(true)

If you wish to still have a back button, you can create a custom one with:

.navigationBarItems(leading: Button("Back"){self.presentationMode.wrappedValue.dismiss()})

Note: this code is placed in the presenting view and remember to do

@Environment(\.presentationMode) var presentationMode

in your struct for iOS 14 and before,
or if you are on iOS 15 you can simply do:

@Environment(\.dismiss) var dismiss

And simply call dismiss() in your custom back button:

.navigationBarItems(leading: Button("Back"){dismiss()})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文