ale*_*ith 1 button textfield ios swiftui
如果 Swift UI 的文本字段为空,我如何禁用按钮。我的代码:
struct AddTask: View {
@Binding var isOpen: Bool
@State var text = ""
let tint: Color
let done: (String) -> ()
var body: some View {
VStack() {
HStack {
Text("What tasks are you planning to do?")
.font(.custom("Avenir", size: 14))
.foregroundColor(.gray)
Spacer()
}
CustomTextField(text: $text, isFirstResponder: true)
.frame(height: 30)
HStack {
Spacer()
Button(action: { self.done(self.text); self.isOpen.toggle() }) { Text("Done") }
.foregroundColor(self.tint)
}
Spacer()
}
.padding(.all, 30)
}
}
Run Code Online (Sandbox Code Playgroud)
如果 CustomTextField 为空,我想禁用按钮。
使用.disabled如下修饰符
Button(action: { self.done(self.text); self.isOpen.toggle() }) { Text("Done") }
.foregroundColor(self.tint)
.disabled(self.text.isEmpty)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1552 次 |
| 最近记录: |