Skip to content

Adding constraints to Popup View on iOS#1979

Merged
bijington merged 7 commits into
CommunityToolkit:mainfrom
cat0363:Issue-1974
Sep 9, 2024
Merged

Adding constraints to Popup View on iOS#1979
bijington merged 7 commits into
CommunityToolkit:mainfrom
cat0363:Issue-1974

Conversation

@cat0363
Copy link
Copy Markdown
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

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
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Resize popup problem on iOS

3 participants