Skip to content

Instantly share code, notes, and snippets.

@devs-rootstrap
Last active October 17, 2023 17:00
Show Gist options
  • Save devs-rootstrap/acded95605fa74f62a45012a15e8f81c to your computer and use it in GitHub Desktop.
Save devs-rootstrap/acded95605fa74f62a45012a15e8f81c to your computer and use it in GitHub Desktop.
SwiftUI: GeometryReader examples
import SwiftUI
struct ContentView: View {
var body: some View {
GeometryReader { geometry in
VStack(spacing: 10) {
Text("GeometryReader")
.font(.title)
Text("Buttons example")
.font(.subheadline)
HStack() {
Button("Button 1") {}
.frame(width: geometry.size.width/3, height: 50)
.background(.orange)
Button("Button 2") {}
.frame(width: geometry.size.width/3, height: 50)
.background(.yellow)
Button("Button 3") {}
.frame(width: geometry.size.width/3, height: 50)
.background(.green)
}
}
}
}
}
import SwiftUI
struct ContentView: View {
var body: some View {
GeometryReader { geometry in
VStack {
Spacer()
ZStack {
Rectangle()
.foregroundColor(.blue)
Circle()
.foregroundColor(.white)
.frame(
width: geometry.size.width * 0.5,
height: geometry.size.width * 0.5
)
}
Spacer()
}
}
}
}
import SwiftUI
struct ContentView: View {
var body: some View {
GeometryReader { geometry in
HStack(alignment: .center) {
Rectangle()
.frame(width: geometry.size.width / 2, height: geometry.size.height / 2)
.foregroundColor(.red)
.alignmentGuide(.leading) { d in d[HorizontalAlignment.center] }
Rectangle()
.frame(width: geometry.size.width / 2, height: geometry.size.height / 2)
.foregroundColor(.blue)
.alignmentGuide(.trailing) { d in d[HorizontalAlignment.center] }
}
}
.padding(10)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment