Skip to content

Avoid KVO on CALayer by introducing an Apple PlatformInterop #30861

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

albyrock87
Copy link
Contributor

Description of Change

On a recent macios workload, a regression caused issues with WeakReferences bringing lots of crashes in our app, all tied to KVOs.

So, once again I felt the fragility of key-value-observers.

This PR introduces an .xcframework for Apple platforms which could really help in the future also for other purposes.

For now, I swizzled CALayer.setBounds methods and added a new platformAutoresizingMask property.

This enables us to do things like:

void IAutoSizableCALayer.AutoSizeToSuperLayer()
{
	Frame = SuperLayer?.Bounds ?? throw new InvalidOperationException("SuperLayer should be set before invoking AutoSizeToSuperLayer");

	this.SetAutoresizeMask(UIViewAutoresizing.FlexibleDimensions);
}

Where the extension method is simply doing:

internal static void SetAutoresizeMask(this CALayer layer, UIViewAutoresizing mask)
{
    layer.SetValueForKey(NSObject.FromObject(mask), new NSString("platformAutoresizingMask"));
}

... I could have added ApiDefinition.cs C# binding, but I didn't want to expose those symbols publicly.

Security

For security concerns, the MAUI team should use the attached script to re-build the .xcframework and commit that on my behalf like we did with Android in the past.

image

This is how the project looks like:

image

@albyrock87 albyrock87 requested a review from a team as a code owner July 26, 2025 17:59
@dotnet-policy-service dotnet-policy-service bot added the community ✨ Community Contribution label Jul 26, 2025
Copy link
Contributor

Hey there @@albyrock87! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@PureWeen PureWeen requested a review from rolfbjarne July 26, 2025 22:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community ✨ Community Contribution
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant