我试图在不使用 zstack 的情况下将图像添加到 swiftui 中的文本输入中,因为这会导致整个视图出现错误,这是预期的结果,有没有办法在不使用堆栈的情况下添加它们?或者我应该基于它调整整个代码?
HStack {
ZStack {
CustomTextField(placeholder: "Email", text: $email, textColor: .black, isValid: emailValid)
.focused($isEmailFocused)
if let isValid = emailValid {
Image(isValid ? "check_solid" : "cancel_solid")
.resizable()
.frame(width: 25, height: 25)
.foregroundColor(isValid ? Color("greenstrokes") : Color("redstrokes"))
.position(x: UIScreen.main.bounds.width - 100, y: 75)
}
}
}
我认为最好的方法是使用这样的覆盖:
VStack(alignment: .leading, spacing: 3) {
HStack {
CustomTextField(placeholder: "Email", text: $email, textColor: .black, isValid: emailValid)
.focused($isEmailFocused)
.overlay(
Group {
if let isValid = emailValid {
Image(isValid ? "check_solid" : "cancel_solid")
.resizable()
.frame(width: 25, height: 25)
.foregroundColor(isValid ? Color("greenstrokes") : Color("redstrokes"))
.padding(.trailing, 8)
}
},
alignment: .trailing
)
}