繁体   English   中英

如何在 SwiftUI 中单击按钮时添加 UI 组件?

[英]How to add UI component on button click in SwiftUI?

在此处输入图片说明 我想在单击任何按钮时在屏幕上再添加一个 Text 或 TextField(UI 组件)。

我的代码:

Button(action: { 

        Text("Adding Text after button clicked") 
        .font(.largeTitle) .fontWeight(.light) 
        .foregroundColor(Color.green) 

}) { Text("Add Text") }

您可以根据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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM