How to set height and width in proportion with superview in SwiftUI?

I want to know that is it possible to adjust

height
and
width
in proportion according to superview in
SwiftUI
to any
UIControl
?

As we are doing like :

  
 
let txtFld = UITextField() 
txtFld.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: self.view.frame.width/2, height: self.view.frame.height*0.1))

I know we can somehow achieve to adjust width by using

Spacer()
,
.padding
and
edgeInsets
. But what about
height
? I have seen lots of tutorial for
SwiftUI
, everywhere
height
is static. Even on Apple's site. SwiftUI Tutorials

If anyone have knowledge about how to set height in proportion with view's height, please share here. Because at some point we need like that. For ex. If it needs to make button according to device height. (Let's say in iPhone SE = 40, iPhone 8P = 55, iPhone X = 65)

Accepted Reply

You can wrap your UI code in a GeometryReader to get access to the width & height of the area in which it's being drawn:


GeometryReader { geometry in
    Rectangle()
        .background(Color.blue)
        .frame(height: geometry.size.height*0.5, width: geometry.size.height*0.5)
}

Replies

You can wrap your UI code in a GeometryReader to get access to the width & height of the area in which it's being drawn:


GeometryReader { geometry in
    Rectangle()
        .background(Color.blue)
        .frame(height: geometry.size.height*0.5, width: geometry.size.height*0.5)
}