-
-
Notifications
You must be signed in to change notification settings - Fork 413
Visibility Modifier
Vatsal Manot edited this page May 29, 2020
·
2 revisions
The way to mark views as hidden in SwiftUI is a bit obtuse, currently.
struct Foo: View {
var body: some View {
Text("Foo")
}
}
struct ContentView: View {
@State var isHidden: Bool = false
var body: some View {
Group {
if isHidden {
Foo().hidden()
} else {
Foo()
}
}
}
}
Additionally, the if
/else
branch in the example above can lead to performance issues when re-rendering complex SwiftUI view hierarchies.
extension View {
/// Sets a view's visibility.
///
/// The view still retains its frame.
public func visible(_ isVisible: Bool = true) -> some View
}
Usage:
struct Foo: View {
var body: some View {
Text("Foo")
}
}
struct ContentView: View {
@State var isHidden: Bool = false
var body: some View {
Foo().visible(!isHidden)
}
}
If you see any problems with the content above, please file an issue here.