Faced a similar problem today. Pay attention to the onChange() block.
Solution:
struct MyView: View {
let realSizeTmp: CGSize
@State var realSize: CGSize
init(containerSize: CGSize) {
let newSize = CGSize(width: containerSize.width * 0.4, height: containerSize.height)
_realSize = State(initialValue: newSize)
realSizeTmp = newSize
}
var body: some View {
ZStack {
}
.onChange(of: realSizeTmp) { newValue in
realSize = newValue
}
.frame(width: realSize.width, height: realSize.height)
.background(Color.red)
}
}