Skip to content

Instantly share code, notes, and snippets.

@chriseidhof
Created February 21, 2020 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chriseidhof/91c7eafb0d3b7292d4394fc464a5cd65 to your computer and use it in GitHub Desktop.
Save chriseidhof/91c7eafb0d3b7292d4394fc464a5cd65 to your computer and use it in GitHub Desktop.
Relative Positioning
//
// ContentView.swift
// Clocks
//
// Created by Chris Eidhof on 21.02.20.
// Copyright © 2020 objc.io. All rights reserved.
//
import SwiftUI
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))
}
}
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