I am trying to create a ButtonView where i have 2 buttons at the bottom of the page: “Next” and “Previous”. The issue i am having is if the user reaches the last page in the loop, i want to be able to hide the “Next” button so that only the “Previous” button shows only when on last page and both buttons show when on any other page other than the last page.
Below is the code i have for my ButtonsView:
import SwiftUI
struct ButtonsView: View {
@Binding var selection: Int
let buttons = ["Previous", "Next"]
var body: some View {
HStack {
ForEach(buttons, id: .self) { buttonLabel in
Button(action: { buttonAction(buttonLabel) }, label: {
Text(buttonLabel)
.font(.system(size: 25))
.fontWeight(.heavy)
.padding()
.frame(width: 150, height: 44)
.background(Color.black.opacity(0.27))
.cornerRadius(12)
.padding(.horizontal)
})
}
}
.foregroundColor(.yellow)
.padding()
}
func buttonAction(_ buttonLabel: String) {
withAnimation {
if buttonLabel == "Previous" && selection > 0 {
selection -= 1
} else if buttonLabel == "Next" && selection < pages.count - 1 {
selection += 1
}
}
}
}
In the buttonAction func, i believe is where we need to implement the code to hide the “Next” button if we reach the last page but i am unaware of how to do so.
What code would i implement to hide the “Next” button when i reach the last page and unhide the “Next” button when we are NOT on last page?