Skip to content

[iOS] Fix SafeArea arrange insets using incorrect bounds in SizeThatFits #30457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 7, 2025

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Description

Fixes an issue where SafeArea arrange insets were incorrectly calculated due to a mismatch between the measure and arrange phases in MauiView on iOS.

Problem

The issue occurred because SizeThatFits and LayoutSubviews were using inconsistent bounds calculations for SafeArea:

  1. SizeThatFits: Measured content without considering SafeArea insets, returning a size based on content alone
  2. LayoutSubviews: Applied SafeArea insets during arrange using AdjustForSafeArea, reducing the available space

This mismatch caused the parent view to allocate space based on the unadjusted size, but the child layout would arrange within the SafeArea-adjusted (smaller) bounds, leading to incorrect layout positioning.

Example

Given this XAML:

<VerticalStackLayout x:Name="layout" 
                     BackgroundColor="Purple" 
                     IgnoreSafeArea="False" 
                     VerticalOptions="Start" 
                     IsClippedToBounds="True">
    <Entry x:Name="entry" 
           BackgroundColor="Green" 
           Text="Hello there" />
</VerticalStackLayout>

The Entry would be positioned incorrectly within the VerticalStackLayout due to the bounds calculation mismatch.

Solution

Modified SizeThatFits in MauiView.cs to:

  1. Detect SafeArea requirements: Uses the same logic as AdjustForSafeArea to determine when SafeArea should be applied
  2. Adjust measurement constraints: Reduces the available measurement space by SafeArea insets before measuring content
  3. Return correct total size: Adds back the SafeArea insets to the measured size so the parent allocates the correct total space

This ensures consistency between the measure and arrange phases, with both accounting for SafeArea insets appropriately.

Changes

  • Modified SizeThatFits method in src/Core/src/Platform/iOS/MauiView.cs
  • Added comprehensive UI test case Issue24246 to validate the fix
  • Minimal changes: 30 lines added, 1 removed in the core implementation

Testing

  • Created test case reproducing the original problem scenario
  • Verified compilation and build success across multiple target frameworks
  • Ensured no regressions in non-SafeArea scenarios
  • Applied code formatting and style guidelines

Fixes #24246.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

…hatFits

Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] [iOS] SafeArea arrange insets are currently insetting based on an incorrect Bounds [iOS] Fix SafeArea arrange insets using incorrect bounds in SizeThatFits Jul 7, 2025
@Copilot Copilot AI requested a review from PureWeen July 7, 2025 02:50
Copilot finished work on behalf of PureWeen July 7, 2025 02:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[iOS] SafeArea arrange insets are currently insetting based on an incorrect Bounds
2 participants