Skip to content

Instantly share code, notes, and snippets.

@chriseidhof
Created March 2, 2020 10:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chriseidhof/c53605fd1cab8ef2623470fc6151f633 to your computer and use it in GitHub Desktop.
Save chriseidhof/c53605fd1cab8ef2623470fc6151f633 to your computer and use it in GitHub Desktop.
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