Skip to content

Instantly share code, notes, and snippets.

@nicklockwood
Created May 8, 2025 17:20
Show Gist options
  • Save nicklockwood/0e46304ef8bb64f47f7a22ce00ec2f23 to your computer and use it in GitHub Desktop.
Save nicklockwood/0e46304ef8bb64f47f7a22ce00ec2f23 to your computer and use it in GitHub Desktop.
Solution for checking if a given view is an "empty view"
import SwiftUI
public extension View {
var isEmpty: Bool {
(self as? IsEmptyView)?.isEmpty ?? false
}
}
private protocol IsEmptyView {
var isEmpty: Bool { get }
}
extension EmptyView: IsEmptyView {
var isEmpty: Bool { true }
}
extension _ConditionalContent: IsEmptyView {
var isEmpty: Bool {
switch self.storage {
case .trueContent(let content):
(content as? IsEmptyView)?.isEmpty ?? false
case .falseContent(let content):
(content as? IsEmptyView)?.isEmpty ?? false
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment