[英]How to add UI component on button click in SwiftUI?
I want to add one more Text or TextField (UI Component) to the screen on clicking any button.
我想在单击任何按钮时在屏幕上再添加一个 Text 或 TextField(UI 组件)。
My Code:我的代码:
Button(action: {
Text("Adding Text after button clicked")
.font(.largeTitle) .fontWeight(.light)
.foregroundColor(Color.green)
}) { Text("Add Text") }
You can add random UI based on click of Button
:您可以根据
Button
点击添加随机 UI:
struct ContentView: View {
@State var isAddUI: Bool = false
var body: some View {
VStack(spacing: 20) {
Text("Hello..").font(.largeTitle).fontWeight(.bold)
if isAddUI {
Text("Adding Text after button clicked")
.font(.largeTitle)
.fontWeight(.regular)
}
Button(action: {
self.isAddUI = true
}) { Text("Add Text") }
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.