Created
May 8, 2025 17:20
-
-
Save nicklockwood/0e46304ef8bb64f47f7a22ce00ec2f23 to your computer and use it in GitHub Desktop.
Solution for checking if a given view is an "empty view"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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