Last active
January 21, 2021 08:14
-
-
Save chriseidhof/7f148be1f2dc810f669d8e2b0ce7943a to your computer and use it in GitHub Desktop.
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 | |
// WeatherWidget | |
// | |
// Created by Chris Eidhof on 20.01.21. | |
// | |
import SwiftUI | |
struct MyGuide: AlignmentID { | |
static func defaultValue(in context: ViewDimensions) -> CGFloat { | |
context.height/2 | |
} | |
} | |
extension VerticalAlignment { | |
static let myGuide = VerticalAlignment(MyGuide.self) | |
} | |
struct WeatherWidget: View { | |
var body: some View { | |
HStack(alignment: .myGuide) { | |
Circle().fill(Color.orange) | |
.frame(width: 50, height: 50) | |
Spacer() | |
VStack(alignment: .leading) { | |
Text("San Francisco") | |
HStack { | |
VStack(alignment: .leading) { | |
Text("Sunrise") | |
.alignmentGuide(.myGuide, computeValue: { dimension in | |
dimension[VerticalAlignment.center] | |
}) | |
Text("Sunset") | |
}.opacity(0.8) | |
VStack { | |
Text("08:05") | |
.alignmentGuide(.myGuide, computeValue: { dimension in | |
dimension[VerticalAlignment.center] | |
}) | |
Text("16:29") | |
}.font(Font.body.bold()) | |
} | |
}.font(Font.body) | |
Spacer() | |
Text("☁️").font(.title) | |
Spacer() | |
Text("2°").font(.title) | |
} | |
.padding(10) | |
.background(RoundedRectangle(cornerRadius: 10).fill(Color.blue)) | |
.padding() | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
WeatherWidget() | |
.frame(width: 300) | |
.frame(maxHeight: 150) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment