Proportional height (or width) in SwiftUI Proportional height (or width) in SwiftUI swift swift

Proportional height (or width) in SwiftUI


You can make use of GeometryReader. Wrap the reader around all other views and use its closure value metrics to calculate the heights:

let propHeight = metrics.size.height * 0.43

Use it as follows:

import SwiftUIstruct ContentView: View {    var body: some View {        GeometryReader { metrics in            VStack(spacing: 0) {                Color.red.frame(height: metrics.size.height * 0.43)                Color.green.frame(height: metrics.size.height * 0.37)                Color.yellow            }        }    }}import PlaygroundSupportPlaygroundPage.current.liveView = UIHostingController(rootView: ContentView())