禁用 SwiftUI NavigationView 滑动到弹出功能
我正在使用Swiftui NavigationView导航到三页。 导航工作正常,我可以转到所需的页面,然后使用后面按钮返回上一页。
我的问题是,每当我从屏幕的最左侧部分滑动时,它都会弹出当前的屏幕。
每当我从屏幕的最左侧部分拖动时,我当前的屏幕也会沿着显示堆栈的上一个屏幕移动,然后弹出上述当前屏幕。
有没有办法禁用此功能?
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?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
截至目前,禁用滑动关闭的唯一方法是禁用后退按钮:
如果您希望仍然有后退按钮,您可以创建一个自定义按钮:
注意:此代码放置在呈现视图中并记住 执行的操作
在 iOS 14 及之前的结构中 ,
或者,如果您使用的是 iOS 15,您可以简单地执行以下操作:
并在自定义后退按钮中调用miss():
As of now, the only way to disable the swipe to dismiss is to disable the back button:
If you wish to still have a back button, you can create a custom one with:
Note: this code is placed in the presenting view and remember to do
in your struct for iOS 14 and before,
or if you are on iOS 15 you can simply do:
And simply call dismiss() in your custom back button: