I have custom Button Components that can trigger an action but I do want to navigate after the action is completed.
I tried putting the button component into the NavigationLink but it doesn't navigate after all and the button loses its touch animation.
The View:
struct LoginView: View {
// initializers ---
let K = Constants()
// States ---
@State private var email: String = ""
@State private var isLoggedIn = false
var body: some View {
VStack(alignment: .leading) {
// other UI components
CustomTextField(
email: $email,
imageName: "envelope",
placeholder: "Enter e-mail address"
)
// other UI components
FullWidthButton(
btnLable: "Continue with Apple",
onPressHandler: onAppleLogin,
backlgroundColor: Color(K.black),
prefixIconName: K.appleLogo
)
FullWidthButton(
btnLable: "Continue with Facebook",
onPressHandler: onFaceBookLogin,
backlgroundColor: Color(K.fbBlue),
prefixIconName: K.fbLogo
)
Spacer()
// this implementation cause the loss of touch animation and didn't even navigate
NavigationLink {
HomeView()
} label: {
FullWidthButton(
btnLable: "Continue",
onPressHandler: onContinuePress,
backlgroundColor: Color(K.primary)
)
}
.simultaneousGesture(TapGesture().onEnded({ _ in
onContinuePress()
}))
}
}
// submit handler triggered when Continue btn pressed
func onContinuePress() {
print("aslkdfja")
}
}
The button component
struct FullWidthButton: View {
// initialisers ---
let K = Constants()
// porperties ---
var btnLable: String
var onPressHandler: () -> Void
var backlgroundColor: Color
var prefixIconName: String?
var body: some View {
Button {
onPressHandler()
} label: {
ZStack{
RoundedRectangle(cornerRadius: 27.0)
.fill(backlgroundColor)
.frame(height: 54)
.padding(.horizontal)
.overlay(alignment: .center) {
HStack(alignment: .center) {
if let imageName = prefixIconName {
Image(imageName)
}
Text(btnLable)
.font(.custom(K.poppins, size: 16))
.fontWeight(.medium)
.foregroundColor(.white)
}
}
}
}
.padding(.bottom)
}
}
I want to call the function first and then navigate to the homeView() based on the result of the function. I've tried to put the FullWidthButton() into the NavigationLink(destinatin:label:) but it doesn't navigate to the View. I've searched on google and some posts showed me to use NavigationLink() with isActive:
flag but it gives me warning of deprecated in ios 16.