Skip to content
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

Adding constraints to Popup View on iOS #1979

Merged
merged 7 commits into from
Sep 9, 2024

Conversation

cat0363
Copy link
Contributor

@cat0363 cat0363 commented Jun 25, 2024

This PR resolves the issue where the Popup size does not change according to the change in the Popup content size.

Description of Change

Currently, there are no constraints, so it is not possible to change the size of the Popup in response to changes in the size of the Popup's content. First, add a constraint so that the ViewDidLayoutSubviews method is called as the Popup's content changes.

[src\CommunityToolkit.Maui.Core\Views\Popup\MauiPopup.macios.cs]

void SetView(UIView view, IPlatformViewHandler control)
{
	view.AddSubview(control.ViewController?.View ?? throw new InvalidOperationException($"{nameof(control.ViewController.View)} cannot be null."));
	view.Bounds = new CGRect(0, 0, PreferredContentSize.Width, PreferredContentSize.Height);
	AddChildViewController(control.ViewController);

	view.SafeTopAnchor().ConstraintEqualTo(control.ViewController.View.SafeTopAnchor()).Active = true;
	view.SafeBottomAnchor().ConstraintEqualTo(control.ViewController.View.SafeBottomAnchor()).Active = true;
	view.SafeLeadingAnchor().ConstraintEqualTo(control.ViewController.View.SafeLeadingAnchor()).Active = true;
	view.SafeTrailingAnchor().ConstraintEqualTo(control.ViewController.View.SafeTrailingAnchor()).Active = true;

	if (VirtualView is not null)
	{
		this.SetBackgroundColor(VirtualView);
	}
}

Next, within the ViewDidLayoutSubviews method, call methods to resize and layout the Popup.

[src\CommunityToolkit.Maui.Core\Views\Popup\MauiPopup.macios.cs]

public override void ViewDidLayoutSubviews()
{
	base.ViewDidLayoutSubviews();

	_ = View ?? throw new InvalidOperationException($"{nameof(View)} cannot be null.");

	View.Superview.Layer.CornerRadius = 0.0f;
	View.Superview.Layer.MasksToBounds = false;

	if (PresentationController is not null)
	{
		SetShadowView(PresentationController.ContainerView);
	}

	SetElementSize(new Size(View.Bounds.Width, View.Bounds.Height));

	if (VirtualView is not null)
	{
		PopupExtensions.SetSize(this, VirtualView);
		PopupExtensions.SetLayout(this, VirtualView);
	}
}

With this fix, the size of the Popup itself will change according to the change in the size of the Popup's Content.

Linked Issues

PR Checklist

  • Has a linked Issue, and the Issue has been approved(bug) or Championed (feature/proposal)
  • Has tests (if omitted, state reason in description)
  • Has samples (if omitted, state reason in description)
  • Rebased on top of main at time of PR
  • Changes adhere to coding standard
  • Documentation created or updated: https://github.com/MicrosoftDocs/CommunityToolkit/pulls

Additional information

Below is the verification video. For verification, we used the source code provided in Issue #1974.

iPhone.15.iOS.17.0.2024-06-25.11-24-23.mp4

You can see that the size of the Popup itself changes according to the change in the size of the Popup's Content.

The following is the verification result when HorizontalOptions and VerticalOptions are set to End.

iPhone.15.iOS.17.0.2024-06-25.11-36-27.mp4

The following is the verification result when HorizontalOptions and VerticalOptions are set to Start.

iPhone.15.iOS.17.0.2024-06-25.11-38-25.mp4

You can see that both cases give the expected results.

The following is the verification result when the Popup size is explicitly specified as (300, 300).

iPhone.15.iOS.17.0.2024-06-25.11-42-02.mp4

You can see that the size of the Popup itself does not change as the size of the Popup's Content changes, and that the size specified for the Popup takes precedence.

@bijington bijington enabled auto-merge (squash) September 9, 2024 06:07
@bijington bijington merged commit 6651d77 into CommunityToolkit:main Sep 9, 2024
7 checks passed
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.

[BUG] Resize popup problem on iOS
3 participants