[英]Picture and text when clicking on the button. SwiftUI
How to make a picture and text appear when you click on the button?如何让点击按钮时出现图片和文字?
VStack {
Image(systemName: "house.fill").resizable().aspectRatio(contentMode: .fill) .frame(width: 75, height: 75)
Text("TEST")
Spacer().frame(height: 100)
Button(action: { }) {
Text("TEST").foregroundColor(Color.white).padding()
}.background(Color.black) .cornerRadius(10)
}
I'm new to swift, Thanks for any help我是 swift 的新手,感谢您的帮助
You can use an if
clause to conditionally display elements based on a @State
variable.您可以使用
if
子句根据@State
变量有条件地显示元素。
I'm using the extra frame
modifier on the VStack
that you didn't originally have to make sure that nothing changes position when the @State
variable changes and the elements inside the stack appear/disappear.我在
VStack
上使用了额外的frame
修饰符,您最初不必确保当@State
变量发生更改并且堆栈内的元素出现/消失时,位置不会发生任何变化。
struct ContentView : View {
@State private var showImage = false
var body: some View {
VStack {
VStack {
if showImage {
Image(systemName: "house.fill").resizable().aspectRatio(contentMode: .fill).frame(width: 75, height: 75)
Text("TEST")
}
}.frame(height: 100)
Spacer().frame(height: 100)
Button(action: { showImage.toggle() }) {
Text("TEST").foregroundColor(Color.white).padding()
}.background(Color.black) .cornerRadius(10) }
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.