it's really a simple demo here, when I use NavigationLink destination, go to ContentView3(),i show a custom alert, when it show up, the navigation back button still work.what can I do, when my alert show , and set the back button clickable to false?(the alert must be a custom one, cannot use system .alert)
import SwiftUI
struct ContentView: View {
@State var isActive : Bool = false
var body: some View {
NavigationView {
NavigationLink(
destination: ContentView2(rootIsActive: self.$isActive),
isActive: self.$isActive
) {
Text("Hello, World!")
}
.isDetailLink(false)
.navigationBarTitle("Root")
}.navigationViewStyle(.stack)
}
}
struct ContentView2: View {
@Binding var rootIsActive : Bool
var body: some View {
NavigationLink(destination: ContentView3(shouldPopToRootView: self.$rootIsActive)) {
Text("Hello, World #2!")
}
.isDetailLink(false)
.navigationBarTitle("Two")
}
}
struct ContentView3: View {
@Binding var shouldPopToRootView : Bool
@State var showTip = false
var body: some View {
ZStack {
VStack {
Text("Hello, World #3!")
Button (action: {
self.shouldPopToRootView = false }
)
{
Text("Pop to root")
}
Button (action: {
self.showTip.toggle() }
)
{
Text("show tip")
}
}
// when alert show up, the back button still work, what can I do stop it? let the back button clickable to false?
if showTip {
TipAlert(isPresented: $showTip, tip: "123445", negative: "", positive: "OK") {
}
}
}
.navigationBarTitle("Three")
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct TipAlert: View {
@Binding var isPresented: Bool
let tip: String
let negative: String
let positive: String
let action: (() -> Void)
var body: some View {
ZStack {
Color.gray.edgesIgnoringSafeArea(.all).opacity(0.75)
VStack(spacing: 10) {
Text(tip)
.font(.system(size: kFrontSize_18))
.foregroundColor(.black)
HStack (alignment: .center, spacing: 20){
Spacer()
if !negative.isEmpty {
Button {
isPresented.toggle()
} label: {
Text(negative)
.font(.system(size: kFrontSize_15))
.padding(.horizontal, 12)
.padding(.vertical, 8)
.frame(minWidth: 55)
.foregroundColor(.white)
.clipShape(RoundedRectangle(cornerRadius: 5))
}
}
Button {
action()
isPresented.toggle()
} label: {
Text(positive)
.font(.system(size: kFrontSize_15))
.padding(.horizontal, 12)
.padding(.vertical, 8)
.frame(minWidth: 55)
.foregroundColor(.white)
.clipShape(RoundedRectangle(cornerRadius: 5))
}
Spacer()
}
}
.padding(10)
.background(.white)
.frame(
width: 340
)
.cornerRadius(5)
}
}
}