What exactly is going on in this SwiftUI code I wonder. I want to align the blue view to the leading edge of the safe area in the landscape mode. The below code works but if I remove -0.5 points adjustment in the calculation of blueOffset below, it simply doesn't works. The Blue View simply fills the whole safe area on the leading edge. I wonder what magic -0.5 does?
@ViewBuilder
var secondBody:some View {
Color.green.opacity(0.3)
.ignoresSafeArea()
.onGeometryChange(for: CGRect.self) { proxy in
proxy.frame(in: .global)
} action: { newValue in
viewSize = newValue.size
}
.background {
Color.blue
.ignoresSafeArea()
.aspectRatio(verticalSizeClass == .regular ? 9.0/16.0 : 16.0/9.0, contentMode: .fit)
.onGeometryChange(for: CGRect.self) { proxy in
proxy.frame(in: .global)
} action: { newValue in
blueViewSize = newValue.size
//This line, why it works????
blueOffset =
verticalSizeClass == .regular ? 0 :
(viewSize.width - blueViewSize.width)/2 - 0.5
print("Blue offset \(blueOffset)")
}
.offset(x: -blueOffset)
}
.persistentSystemOverlays(.hidden)
}
.ignoresSafeArea()onColor.blue?.ignoresSafeArea(edges: [.vertical, .trailing])perhaps?.ignoresSafeArea()modifiers. You can simply have one , if you move it at the end, after.persistentSystemOverlay(.hidden). This way, it will apply to everything above.