Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
514 views
in Technique[技术] by (71.8m points)

SwiftUI - disable NavigationView back button menu

So, I have the following code:

struct ContentView: View {
    var body: some View {
        NavigationView {
            NavigationLink("Page", destination: PageView())
                .font(Font.system(.largeTitle))
        }
    }
}

struct PageView: View {
    var body: some View {
        Text("Hello, world!")
    }
}

When the "Page" NavigationLink is selected, you are redirected to a new screen (PageView). When you long-press (hold) the back button to go back to the main screen (ContentView), a menu appears (new feature in iOS14+):

enter image description here

Is there a way to disable the menu popup on a long-press gesture, using SwiftUI (without adding custom back button)?

Cheers everyone, thanks for your time!

question from:https://stackoverflow.com/questions/65907543/swiftui-disable-navigationview-back-button-menu

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I was able to find a workaround. Not pretty, but it works ^^.

First I was trying to use Introspect library to change the underlying UIKit components since there is a Solution to your problem for UIKit. But it didn't turn out well.

So here comes the workaround.

Idea

Overlay the back button with an invisible view that has the same tap gesture, but no long press for the menu.

Solution

Finding out and reaching the exact position of the back button independent of user device type.

image image

Code:

struct ContentView: View {
    
    @State private var showPageView = false
    
    var body: some View {
        NavigationView {
            NavigationLink("Page", destination: PageView(), isActive: $showPageView)
                .font(Font.system(.largeTitle))
        }
        .overlay(NavigationBackButtonHiddenTouchView(isActive: $showPageView))
    }
}

struct PageView: View {
    
    var body: some View {
        ZStack {
            Text("Hello, world!")
        }
    }
}

struct NavigationBackButtonHiddenTouchView: View {
    
    @Binding var isActive: Bool
    
    var body: some View {
        VStack {
            HStack {
                Color.black.opacity(0.0000001).frame(width: 80, height: 35)
                    .onTapGesture { isActive = false }
                
                Spacer()
            }
            .frame(height: navBarHeight)
            
            Spacer()
        }
    }
    
    var navBarHeight: CGFloat = {
        return UINavigationController().navigationBar.frame.size.height
    }()
}

Problem

Making it reusable for further navigation links (you need to handle it manually, ugly but possible)

You might also have to handle hiding NavigationBackButtonHiddenTouchView from your parent view, if it causes complications.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...