Whenever I like to show or hide view elements, I stumble over the hidden()
view property:
struct MyView: View {
var body: some View {
Text("Visible")
Text("Not visible")
.hidden()
}
}
What perplexes me is why anyone would need this? If it is always hidden, then why add it in the first place??
Well, I guess it could be used to create some well-defined, empty space in the view layout.
Now, this is what I would really have a use for:
struct MyView: View {
var show_info: Bool
var body: some View {
Text("Visible")
Text("Sometimes visible")
.hide(!show_info) // OBS! Doesn't exist!
}
}
Depending on whether or not the hidden part should be used when laying out the view, a different approach is needed. Either without taking any space:
struct MyView: View {
var show_info: Bool
var body: some View {
Text("Visible")
if show_info {
// Not occupying space when hidden
Text("Sometimes visible")
}
}
}
Or occupying space:
struct MyView: View {
var show_info: Bool
var body: some View {
Text("Visible")
// Taking space, even if transparent
Text("Sometimes visible")
.opacity(show_info ? 1.0 : 0.0)
}
}