Created
February 21, 2020 13:13
-
-
Save chriseidhof/91c7eafb0d3b7292d4394fc464a5cd65 to your computer and use it in GitHub Desktop.
Relative Positioning
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
// | |
// 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