Last active
September 17, 2020 22:05
-
-
Save maximkrouk/eafe497279067df303d30372c5ba0f29 to your computer and use it in GitHub Desktop.
Custom SwiftUI foreground
This file contains 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 | |
// MARK: - API | |
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *) | |
extension View { | |
public func foreground<Overlay: View>(_ overlay: Overlay) -> some View { | |
_Foreground(overlay: overlay, for: self) | |
} | |
} | |
// MARK: - Implementation | |
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *) | |
private struct _Foreground<Content: View, Overlay: View>: View { | |
let content: Content | |
let overlay: Overlay | |
internal init(overlay: Overlay, for content: Content) { | |
self.content = content | |
self.overlay = overlay | |
} | |
var body: some View { | |
content.overlay(overlay).mask(content) | |
} | |
} |
This file contains 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
// I think, that `Foreground.swift` approach is better, but there is an alternative without custom view. | |
import SwiftUI | |
// MARK: - API | |
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *) | |
extension View { | |
public func foreground<Overlay: View>(_ overlay: Overlay) -> some View { | |
self.overlay(overlay).mask(self) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage example
Back to index