/*
Intrinsic Content Size (ideal size):
------------------------------------
๐ธ 1. define ideal size (SwiftUI won't respect it at first).
๐ธ 2. use `.fixedSize()` to respect ideal size.
๐ธ 3. use `.frame(width:height:) to override.
๐ธ 4. can mix (2) / (3)
*/
struct ContentView: View {
var body: some View {
VStack {
rect()
.fixedSize() // โญ๏ธ 2. respect ideal width/height
rect(.blue) // โญ๏ธ 2. respect ideal height
.fixedSize(horizontal: false, vertical: true)
rect(.orange) // โญ๏ธ 2. respect ideal width
.fixedSize(horizontal: true, vertical: false)
rect(.purple) // โญ๏ธ 3. override both
.frame(width: 150, height: 20)
rect(.green) // โญ๏ธ 4. respect height, override width
.fixedSize(horizontal: false, vertical: true)
.frame(width: 200)
}.padding()
}
}
extension ContentView {
func rect(_ color: Color = .pink) -> some View {
Rectangle()
.frame(idealWidth: 100, idealHeight: 100) // 1.
.foregroundColor(color)
}
}