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

Can confirm on iOS 26.0 too. Very annoying. Solved conditionally rendered view by displaying it always and instead hiding it with .opacity(condition ? 1 : 0).

Thank you @simonnickel for reporting, as well as @vojta.bohm for confirming + offering a workaround.

You can see the status of this report in Feedback Assistant. There, you can track if the report is still being investigated, has a potential identifiable fix, or has been resolved in another way. The status appears beside the label "Resolution." We're unable to share any updates on specific reports on the forums.

For more details on when you'll see updates to your report, please see What to expect after submission.

 Travis Trotto - DTS Engineer

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