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.
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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…