Skip to content

Instantly share code, notes, and snippets.

@don1138
Last active May 20, 2023 15:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save don1138/697f5fd271a06df4cabbbfb4e12f0157 to your computer and use it in GitHub Desktop.
Save don1138/697f5fd271a06df4cabbbfb4e12f0157 to your computer and use it in GitHub Desktop.
Use percentage widths on SwiftUI layout objects
// Get screen width and subtract Safe Area margins
var containerWidth:CGFloat = UIScreen.main.bounds.width - 32.0
// Set frame width of each column using (containerWidth * percentage)
HStack (spacing:0) {
HStack {
Text("Column 25% Width")
}
.frame(width: containerWidth * 0.25)
HStack {
Text("Column 25% Width")
}
.frame(width: containerWidth * 0.25)
HStack {
Text("Column 50% Width")
}
.frame(width: containerWidth * 0.5)
}
@afoeder
Copy link

afoeder commented May 20, 2023

thank you, that helped me a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment