File: Handlers\View\ViewHandlerOfT.iOS.cs
Web Access
Project: src\src\Core\src\Core.csproj (Microsoft.Maui)
using Microsoft.Maui.Graphics;
using UIKit;
 
namespace Microsoft.Maui.Handlers
{
	public partial class ViewHandler<TVirtualView, TPlatformView> : IPlatformViewHandler
	{
		public new WrapperView? ContainerView
		{
			get => (WrapperView?)base.ContainerView;
			protected set => base.ContainerView = value;
		}
 
		public UIViewController? ViewController { get; set; }
 
		public override void PlatformArrange(Rect rect) =>
			this.PlatformArrangeHandler(rect);
 
		public override Size GetDesiredSize(double widthConstraint, double heightConstraint) =>
			this.GetDesiredSizeFromHandler(widthConstraint, heightConstraint);
 
		protected override void SetupContainer()
		{
			if (PlatformView == null || ContainerView != null)
				return;
 
			var oldParent = (UIView?)PlatformView.Superview;
 
			var oldIndex = oldParent?.IndexOfSubview(PlatformView);
			PlatformView.RemoveFromSuperview();
 
			ContainerView ??= new WrapperView(PlatformView.Bounds);
			ContainerView.AddSubview(PlatformView);
 
			if (oldIndex is int idx && idx >= 0)
				oldParent?.InsertSubview(ContainerView, idx);
			else
				oldParent?.AddSubview(ContainerView);
		}
 
		protected override void RemoveContainer()
		{
			if (PlatformView == null || ContainerView == null || PlatformView.Superview != ContainerView)
			{
				CleanupContainerView(ContainerView);
				ContainerView = null;
				return;
			}
 
			var oldParent = (UIView?)ContainerView.Superview;
 
			var oldIndex = oldParent?.IndexOfSubview(ContainerView);
			CleanupContainerView(ContainerView);
			ContainerView = null;
 
			if (oldIndex is int idx && idx >= 0)
				oldParent?.InsertSubview(PlatformView, idx);
			else
				oldParent?.AddSubview(PlatformView);
 
			void CleanupContainerView(UIView? containerView)
			{
				if (containerView is WrapperView wrapperView)
				{
					wrapperView.RemoveFromSuperview();
					wrapperView.Disconnect();
				}
			}
		}
	}
}