Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit e8a6c8b

Browse files
committed
Added iOS PopupRenderer and removed old wrapper. This follows a standard renderer pattern
1 parent ef17b6e commit e8a6c8b

File tree

5 files changed

+136
-120
lines changed

5 files changed

+136
-120
lines changed

Xamarin.Forms.Platform.iOS/Platform.cs

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ public class Platform : BindableObject, INavigation, IDisposable
4040
bool _appeared;
4141
bool _disposed;
4242

43-
UIPopoverPresentationController _popoverPresentationController;
44-
4543
internal Platform()
4644
{
4745
_renderer = new PlatformRenderer(this);
@@ -531,44 +529,30 @@ void EndEditing()
531529
_renderer.View?.Window?.EndEditing(true);
532530
}
533531

534-
public async Task<T> ShowPopup<T>(Popup<T> popup)
532+
private class DemoPopoverController : UIViewController
535533
{
536-
var wrapper = PopupWrapper<T>.Wrap(popup);
537-
538-
var currenPageRenderer = GetRenderer(Application.Current.MainPage);
539-
var presentingViewController = currenPageRenderer.ViewController;
540-
541-
var anchorNativeView = popup.Anchor == null ? currenPageRenderer.NativeView : GetRenderer(popup.Anchor).NativeView;
542-
543-
wrapper.ModalInPopover = !popup.IsLightDismissEnabled;
544-
wrapper.ModalPresentationStyle = UIKit.UIModalPresentationStyle.Popover;
545-
546-
presentingViewController.PresentViewController(wrapper, true, null);
547-
548-
_popoverPresentationController = wrapper.PopoverPresentationController;
549-
550-
_popoverPresentationController.DidDismiss += (sender, e) =>
534+
public DemoPopoverController() : base("TestPopover", null) { }
535+
public override void ViewDidLoad()
551536
{
552-
popup.LightDismiss();
553-
_popoverPresentationController = null;
554-
};
555-
556-
_popoverPresentationController.SourceRect = new RectangleF(anchorNativeView.Bounds.Location, anchorNativeView.Bounds.Size);
557-
_popoverPresentationController.SourceView = anchorNativeView;
558-
559-
var result = await popup.Result;
560-
561-
if (!Forms.IsiOS9OrNewer)
562-
{
563-
await presentingViewController.DismissViewControllerAsync(true);
537+
base.ViewDidLoad();
538+
View.Frame = new CoreGraphics.CGRect(0, 0, 100, 100);
539+
this.PreferredContentSize = new CoreGraphics.CGSize(10, 10);
540+
View.BackgroundColor = UIColor.Red;
564541
}
565-
else
542+
543+
public class PopoverDelegate : UIPopoverPresentationControllerDelegate
566544
{
567-
presentingViewController.DismissViewController(true, null);
545+
public override UIKit.UIModalPresentationStyle GetAdaptivePresentationStyle(UIPresentationController forPresentationController)
546+
{
547+
return UIKit.UIModalPresentationStyle.None;
548+
}
568549
}
550+
}
569551

570-
_popoverPresentationController = null;
571-
return result;
552+
public Task<T> ShowPopup<T>(Popup<T> popup)
553+
{
554+
CreateRenderer(popup);
555+
return popup.Result;
572556
}
573557

574558
internal class DefaultRenderer : VisualElementRenderer<VisualElement>

Xamarin.Forms.Platform.iOS/PopupWrapper.cs

Lines changed: 0 additions & 85 deletions
This file was deleted.

Xamarin.Forms.Platform.iOS/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
[assembly: ExportRenderer(typeof(RefreshView), typeof(RefreshViewRenderer))]
4040
#endif
4141

42+
[assembly: ExportRenderer(typeof(Popup), typeof(PopupRenderer))]
4243
[assembly: ExportRenderer(typeof(MasterDetailPage), typeof(TabletMasterDetailRenderer), UIUserInterfaceIdiom.Pad)]
4344
[assembly: ExportRenderer(typeof(NativeViewWrapper), typeof(NativeViewWrapperRenderer))]
4445
[assembly: ExportRenderer(typeof(Shell), typeof(ShellRenderer))]
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
using System;
2+
using CoreGraphics;
3+
using UIKit;
4+
5+
namespace Xamarin.Forms.Platform.iOS
6+
{
7+
public class PopupRenderer : UIViewController, IVisualElementRenderer
8+
{
9+
public IVisualElementRenderer Control { get; private set; }
10+
public Popup Element { get; private set; }
11+
VisualElement IVisualElementRenderer.Element { get => Element; }
12+
public UIView NativeView { get => View; }
13+
public UIViewController ViewController { get => this; }
14+
public event EventHandler<VisualElementChangedEventArgs> ElementChanged;
15+
public void SetElementSize(Size size)
16+
{
17+
Control?.SetElementSize(size);
18+
}
19+
20+
public override void ViewDidLayoutSubviews()
21+
{
22+
base.ViewDidLayoutSubviews();
23+
SetElementSize(new Size(View.Bounds.Width, View.Bounds.Height));
24+
}
25+
26+
public SizeRequest GetDesiredSize(double widthConstraint, double heightConstraint)
27+
{
28+
return NativeView.GetSizeRequest(widthConstraint, heightConstraint);
29+
}
30+
31+
public void SetElement(VisualElement element)
32+
{
33+
var oldElement = Element;
34+
Element = (Popup)element;
35+
36+
OnElementChanged(new ElementChangedEventArgs<Popup>(oldElement, Element));
37+
}
38+
39+
protected virtual void OnElementChanged(ElementChangedEventArgs<Popup> e)
40+
{
41+
if (Control == null)
42+
CreateControl();
43+
else if (Element != null)
44+
{
45+
SetSize();
46+
SetBackgroundColor();
47+
}
48+
49+
ElementChanged?.Invoke(this, new VisualElementChangedEventArgs(e.OldElement, e.NewElement));
50+
}
51+
52+
void CreateControl()
53+
{
54+
var view = Element.View;
55+
var contentPage = new ContentPage { Content = view, Padding = new Thickness(25) };
56+
57+
Control = Platform.CreateRenderer(contentPage);
58+
Platform.SetRenderer(contentPage, Control);
59+
contentPage.Parent = Application.Current.MainPage;
60+
61+
ModalInPopover = true;
62+
ModalPresentationStyle = UIModalPresentationStyle.Popover;
63+
64+
SetSize();
65+
SetBackgroundColor();
66+
SetView();
67+
SetPresentationController();
68+
AddToCurrentPageViewController();
69+
}
70+
71+
void SetSize()
72+
{
73+
if (!Element.Size.IsZero)
74+
{
75+
PreferredContentSize = new CGSize(Element.Size.Width, Element.Size.Height);
76+
((UIPopoverPresentationController)PresentationController).SourceRect = new CGRect(0, 0, Element.Size.Width, Element.Size.Height);
77+
}
78+
}
79+
80+
void SetBackgroundColor()
81+
{
82+
View.BackgroundColor = Element.BackgroundColor.ToUIColor();
83+
((UIPopoverPresentationController)PresentationController).BackgroundColor = Element.BackgroundColor.ToUIColor();
84+
}
85+
86+
void SetView()
87+
{
88+
View.AddSubview(Control.ViewController.View);
89+
View.Bounds = Control.ViewController.View.Bounds;
90+
AddChildViewController(Control.ViewController);
91+
}
92+
93+
void SetPresentationController()
94+
{
95+
var currentPageRenderer = Platform.GetRenderer(Application.Current.MainPage);
96+
97+
((UIPopoverPresentationController)PresentationController).SourceView = currentPageRenderer.ViewController.View;
98+
((UIPopoverPresentationController)PresentationController).PermittedArrowDirections = UIPopoverArrowDirection.Up;
99+
((UIPopoverPresentationController)PresentationController).Delegate = new PopoverDelegate();
100+
}
101+
102+
void AddToCurrentPageViewController()
103+
{
104+
var currentPageRenderer = Platform.GetRenderer(Application.Current.MainPage);
105+
currentPageRenderer.ViewController.PresentViewController(this, true, null);
106+
}
107+
108+
private class PopoverDelegate : UIPopoverPresentationControllerDelegate
109+
{
110+
public override UIModalPresentationStyle GetAdaptivePresentationStyle(UIPresentationController forPresentationController)
111+
{
112+
return UIModalPresentationStyle.None;
113+
}
114+
}
115+
}
116+
}

Xamarin.Forms.Platform.iOS/Xamarin.Forms.Platform.iOS.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,14 @@
182182
<Compile Include="NativeViewWrapper.cs" />
183183
<Compile Include="NativeViewWrapperRenderer.cs" />
184184
<Compile Include="PlatformEffect.cs" />
185-
<Compile Include="PopupWrapper.cs" />
186185
<Compile Include="Renderers\AlignmentExtensions.cs" />
187186
<Compile Include="Forms.cs" />
188187
<Compile Include="PageExtensions.cs" />
189188
<Compile Include="Renderers\IAccessibilityElementsController.cs" />
190189
<Compile Include="Renderers\IShellSectionRootHeader.cs" />
191190
<Compile Include="Renderers\PageContainer.cs" />
192191
<Compile Include="Renderers\CheckBoxRendererBase.cs" />
192+
<Compile Include="Renderers\PopupRenderer.cs" />
193193
<Compile Include="Renderers\RadioButtonCALayer.cs" />
194194
<Compile Include="Renderers\RadioButtonRenderer.cs" />
195195
<Compile Include="Renderers\WkWebViewRenderer.cs" />

0 commit comments

Comments
 (0)