Created
March 2, 2020 10:19
-
-
Save chriseidhof/c53605fd1cab8ef2623470fc6151f633 to your computer and use it in GitHub Desktop.
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
extension View { | |
func vertical(_ key: VerticalAlignment, relative: CGFloat) -> some View { | |
self.alignmentGuide(key, computeValue: { $0.height * relative }) | |
} | |
func overlay<V: View>(alignment: Alignment, relative: UnitPoint = .center, _ other: V) -> some View { | |
self | |
.alignmentGuide(alignment.horizontal, computeValue: { $0.width * relative.x }) | |
.alignmentGuide(alignment.vertical, computeValue: { $0.height * relative.y }) | |
.overlay(other, alignment: alignment) | |
} | |
} | |
struct ContentView: View { | |
var body: some View { | |
Circle() | |
.frame(width: 200, height: 200) | |
.overlay(alignment: .center, relative: UnitPoint(x: 0.3, y: 0.3), Circle().fill(Color.red).frame(width: 30, height: 30)) | |
.overlay(alignment: .center, relative: UnitPoint(x: 0.5, y: 0.75), Text("Hello").foregroundColor(.white)) | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment