简体   繁体   English

单击按钮时的图片和文字。 用户界面

[英]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.

相关问题 按下按钮即可更改UIWebView中的文本。 (简单?) - Changing text in UIWebView with the press of a button. (simple?) SwiftUI:单击按钮时如何显示不同的视图? - SwiftUI: How to show different views when clicking a button? iOS 单击时后退按钮文本消失 - iOS back button text disappears when clicking 带按钮的UIScrollView。 触摸时高亮显示的按钮,仍然滚动滚动视图 - UIScrollView with Button. Button to Highlight when touched, and still scroll the scrollview 从iOS导航后退栏按钮中删除文本。 - Remove text from iOS navigation back bar button. SwiftUI:允许用户通过按下按钮启用画中画 (iOS) - SwiftUI: Allowing user to enable Picture-in-Picture with the press of a button (iOS) 我希望在单击按钮时打开 Siri。 是否可以? - I want the Siri to open when I click button. Is it possible? 单击按钮突出显示 SwiftUI 中的 while 垂直堆栈 - Clicking a button highlights the while vertical stack in SwiftUI 更改布尔值时无法在SwiftUI中更改按钮文本 - Unable to get button text to change in SwiftUI when changing boolean value 点击UIButton作为子视图添加到UILabel打开弹出窗口远离按钮。 - Clicking on UIButton added as subview to UILabel opens popup away from button.?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM