SwiftUI: AlignmentGuide in Overlay not working when in if block

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.

  1. Not working example, where the content is an if-block.
  2. 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

SwiftUI: AlignmentGuide in Overlay not working when in if block
 
 
Q