ideal size
Last updated
Last updated
SwiftUI โฉ Layout โฉ Frame โฉ
้็ถ .frame(minWidth:idealWidth ...) ๅฏไปฅ็จไพๅฎฃๅ ideal size๏ผไฝๅฟ ้ ๆญ้ .fixedSize() ๆๆ็ๆญฃ็ผ็ไฝ็จโ๏ธ
TestIdealSizeView - source code for the YouTube video above.
/*
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)
}
}
SwiftUI โฉ Layout โฉ
.frame(width:height:alignment:) - change proposed size.
.frame(minWidth:idealWidth:maxWidth ...) - define ideal size.
fixedSize(), fixedSize(horizontal:vertical:) - respect ideal size.