I wanted this ErrorView to appear centered on my view. I still find GeometryReaders pretty tricky and I can’t figure out why this ErrorView keeps sticking to the topLeading corner. I’m sure it’s something simple . . . . . help?
struct ErrorView: View {
@State var color = Color.black.opacity(0.7)
@Binding var alert : Bool
@Binding var error : String
var body: some View {
//TODO: Figure out why it's aligning to top left instead of center
GeometryReader { geo in
VStack {
HStack {
Text(self.error == "RESET" ? "Email sent!" : "Error")
.font(.title)
.fontWeight(.bold)
.foregroundColor(self.color)
Spacer()
}
.padding(.horizontal, 25)
Text(self.error == "RESET" ? "Follow the instructions in your email to reset your password." : self.error)
.foregroundColor(self.color)
.padding(.top)
.padding(.horizontal, 25)
Button {
self.alert.toggle()
} label: {
Text(self.error == "RESET" ? "Got it!" : "Cancel")
.foregroundColor(.white)
.padding(.vertical)
.frame(width: UIScreen.main.bounds.width - 120)
}
.background(Color("pink"))
.cornerRadius(10)
.padding(.top, 25)
}
.padding(.vertical, 25)
.frame(width: UIScreen.main.bounds.width - 70)
.background(Color.white)
.cornerRadius(15)
}
.background(Color.black.opacity(0.35).edgesIgnoringSafeArea(.all))
}
}
Where ErrorView is instantiated:
@State var color = Color.black.opacity(0.7)
@State var email = ""
@State var password = ""
@State var isVisible = false
@Binding var show : Bool
@State var alert = false
@State var error = ""
var body: some View {
ZStack {
ZStack(alignment: .topTrailing) { }
if self.alert {
// TODO: Is this why error aligns to top left?
ErrorView(alert: self.$alert, error: self.$error)
}
}
}