Swiftui中的每一个按钮的每次点击都有不同的动作
我正在使用显示/hide 在Swiftui中的视图。当我第一次点击按钮时,它可以正常工作。像这样:
我想要的是,当我第一次点击时,它必须向我展示我的观点。当我点击时,它是 second 时间时,它必须再次隐藏视图。在上,每次点击必须分别显示/hide 视图。在故事板中,此方案很容易处理,但是在 swiftui 中,我不知道该如何做到这一点,因为我是Swiftui的新手。
我的代码是:
import SwiftUI
struct ShowHideText: View {
@State private var showName = false
var body: some View {
VStack(spacing: 50) {
Text("Users")
.fontWeight(.bold)
if showName {
name()
} else {
name()
.hidden()
}
Button("Show/Hide") {
showName = true
}
}
}
func name() -> some View {
VStack(spacing: 30) {
Text("David")
Text("Kate")
}
}
}
struct ShowHideText_Previews: PreviewProvider {
static var previews: some View {
ShowHideText()
}
}
我需要问的另一件事是在我的代码中,如果我使用 .hidded(),则视图 hides ,但是视图的空间仍然存在那里。我不知道它是否有一种隐藏视图的方法,而且我也有视图的内容,因为我也必须在其内容上做一些工作,或者如果我不使用 .hidden()和 name() in else part,我可以对其内容进行一些工作。
I am using a button to show/hide a view in SwiftUI. It is working fine when I first tap the button. Like this:
The thing I want is when I first tap it, it has to show me my view. And when I tap it the second time, it has to again hide the view. And on every tap it has to show/hide the view respectively. In storyboard, this scenario is easy to handle but in SwiftUI, I don't know how to do this as I am new to SwiftUI.
My code is:
import SwiftUI
struct ShowHideText: View {
@State private var showName = false
var body: some View {
VStack(spacing: 50) {
Text("Users")
.fontWeight(.bold)
if showName {
name()
} else {
name()
.hidden()
}
Button("Show/Hide") {
showName = true
}
}
}
func name() -> some View {
VStack(spacing: 30) {
Text("David")
Text("Kate")
}
}
}
struct ShowHideText_Previews: PreviewProvider {
static var previews: some View {
ShowHideText()
}
}
And one more thing which I need to ask is in my code, if I use .hidden(), the view hides but the space of the view remains there. I don't know if there is a way that it hides the view and I also have the view's content as I have to do some work on its content too, or if it works fine if I don't use .hidden() and name() in else part and I can do some work with its content.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更改
为
Change
to
对于您的问题的第二部分。更改
为
For second part of your question. Change
To