Scenario
A SwiftUI view has an overlay with alignment: .top, the content uses .alignmentGuide(.top) {} to adjust the placement.
Issue
When the content of the overlay is in an if-block, the alignment guide is not adjusted.
Example code
The example shows 2 views.
- Not working example, where the content is an if-block.
- Working example, where the content is not in an if-block
Screenshot: https://github.com/simonnickel/FB15248296-SwiftUIAlignmentGuideInOverlayConditional/blob/main/screenshot.png
Tested on
- Xcode Version 16.0 RC (16A242) on iOS 18.0
Code
// Not working
.overlay(alignment: .top) {
if true { // This line causes .alignmentGuide() to fail
Text("Test")
.alignmentGuide(.top, computeValue: { dimension in
dimension[.bottom]
})
}
}
// Working
.overlay(alignment: .top) {
Text("Test")
.alignmentGuide(.top, computeValue: { dimension in
dimension[.bottom]
})
}
Also created a Feedback: FB15248296
Example Project is here: https://github.com/simonnickel/FB15248296-SwiftUIAlignmentGuideInOverlayConditional/tree/main