Created
July 28, 2021 06:48
-
-
Save Dev1an/10b398708a56d8e08ae95cc653e38cea to your computer and use it in GitHub Desktop.
Create an overlay, aligning a guide of the reference view to a different guide of the overlaid view.
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
extension View { | |
@available(iOS 15.0, macOS 12.0, *) | |
func overlay<Target: View>(align originAlignment: Alignment, to targetAlignment: Alignment, of target: Target) -> some View { | |
let hGuide = HorizontalAlignment(Alignment.TwoSided.self) | |
let vGuide = VerticalAlignment(Alignment.TwoSided.self) | |
return alignmentGuide(hGuide) {$0[originAlignment.horizontal]} | |
.alignmentGuide(vGuide) {$0[originAlignment.vertical]} | |
.overlay(alignment: Alignment(horizontal: hGuide, vertical: vGuide)) { | |
target | |
.alignmentGuide(hGuide) {$0[targetAlignment.horizontal]} | |
.alignmentGuide(vGuide) {$0[targetAlignment.vertical]} | |
} | |
} | |
} | |
extension Alignment { | |
enum TwoSided: AlignmentID { | |
static func defaultValue(in context: ViewDimensions) -> CGFloat { 0 } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As a solution for https://stackoverflow.com/questions/68554713/swiftui-reference-the-screen-position-of-another-element-in-the-view/68555127#68555127