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

Commit 459fc4e

Browse files
geskillSebastian Klatte
andauthored
[iOS] Remove popup arrow if anchor isn't set (#854)
* Remove popup arrow if anchor isn't set * Create PopoverArrowDirection.cs * Create Popup.shared.cs * Add arrow direction platform specific. * Add arrow direction platform specific sample. * Rename PopoverArrowDirection.cs to PopoverArrowDirection.shared.cs * Change ArrowDirection to up * Remove switch PopoverArrowDirection.None since default is already zero. * Use div 2 instead of multiply by 0.5 for consistency. Co-authored-by: Sebastian Klatte <[email protected]>
1 parent e1af2ad commit 459fc4e

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

samples/XCT.Sample/Pages/Views/Popups/TransparentPopup.xaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
44
xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
55
xmlns:local="clr-namespace:Xamarin.CommunityToolkit.Sample.Pages.Views.Popups"
6+
xmlns:iosxct="clr-namespace:Xamarin.CommunityToolkit.UI.Views.iOSSpecific;assembly=Xamarin.CommunityToolkit"
67
xmlns:uwpxct="clr-namespace:Xamarin.CommunityToolkit.UI.Views.WindowsSpecific;assembly=Xamarin.CommunityToolkit"
78
Size="{x:Static local:PopupSize.Tiny}"
89
Color="Transparent"
10+
iosxct:Popup.ArrowDirection="Up"
911
uwpxct:Popup.BorderColor="Transparent"
1012
x:Class="Xamarin.CommunityToolkit.Sample.Pages.Views.Popups.TransparentPopup">
1113

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
3+
namespace Xamarin.CommunityToolkit.UI.Views.iOSSpecific
4+
{
5+
public enum PopoverArrowDirection
6+
{
7+
None,
8+
Up,
9+
Down,
10+
Left,
11+
Right,
12+
Any,
13+
Unknown
14+
}
15+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Xamarin.Forms;
2+
3+
namespace Xamarin.CommunityToolkit.UI.Views.iOSSpecific
4+
{
5+
public static class Popup
6+
{
7+
public static readonly BindableProperty ArrowDirectionProperty = BindableProperty.Create(
8+
"ArrowDirection", typeof(PopoverArrowDirection), typeof(Views.Popup), PopoverArrowDirection.None);
9+
10+
public static void SetArrowDirection(BindableObject element, PopoverArrowDirection color) =>
11+
element.SetValue(ArrowDirectionProperty, color);
12+
13+
public static PopoverArrowDirection GetArrowDirection(BindableObject element) =>
14+
(PopoverArrowDirection)element.GetValue(ArrowDirectionProperty);
15+
}
16+
}

src/CommunityToolkit/Xamarin.CommunityToolkit/Views/Popup/iOS/PopupRenderer.ios.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ void SetLayout()
148148
{
149149
var originY = Element.VerticalOptions.Alignment switch
150150
{
151-
LayoutAlignment.End => UIScreen.MainScreen.Bounds.Height - PreferredContentSize.Height,
152-
LayoutAlignment.Center => (UIScreen.MainScreen.Bounds.Height / 2) - (PreferredContentSize.Height / 2),
151+
LayoutAlignment.End => UIScreen.MainScreen.Bounds.Height,
152+
LayoutAlignment.Center => UIScreen.MainScreen.Bounds.Height / 2,
153153
_ => 0f
154154
};
155155

@@ -161,12 +161,24 @@ void SetLayout()
161161
};
162162

163163
PopoverPresentationController.SourceRect = new CGRect(originX, originY, 0, 0);
164+
PopoverPresentationController.PermittedArrowDirections = 0;
164165
}
165166
else
166167
{
167168
var view = Platform.GetRenderer(Element.Anchor).NativeView;
168169
PopoverPresentationController.SourceView = view;
169170
PopoverPresentationController.SourceRect = view.Bounds;
171+
var arrowDirection = Views.iOSSpecific.Popup.GetArrowDirection(Element);
172+
PopoverPresentationController.PermittedArrowDirections = arrowDirection switch
173+
{
174+
Views.iOSSpecific.PopoverArrowDirection.Up => UIPopoverArrowDirection.Up,
175+
Views.iOSSpecific.PopoverArrowDirection.Down => UIPopoverArrowDirection.Down,
176+
Views.iOSSpecific.PopoverArrowDirection.Left => UIPopoverArrowDirection.Left,
177+
Views.iOSSpecific.PopoverArrowDirection.Right => UIPopoverArrowDirection.Right,
178+
Views.iOSSpecific.PopoverArrowDirection.Any => UIPopoverArrowDirection.Any,
179+
Views.iOSSpecific.PopoverArrowDirection.Unknown => UIPopoverArrowDirection.Unknown,
180+
_ => 0
181+
};
170182
}
171183
}
172184

@@ -187,8 +199,6 @@ void SetPresentationController()
187199

188200
((UIPopoverPresentationController)PresentationController).SourceView = ViewController.View;
189201

190-
// Setting PermittedArrowDirector to 0 breaks the Popover layout. It would be nice if there is no anchor to remove the arrow.
191-
((UIPopoverPresentationController)PresentationController).PermittedArrowDirections = UIPopoverArrowDirection.Up;
192202
((UIPopoverPresentationController)PresentationController).Delegate = popOverDelegate;
193203
}
194204

0 commit comments

Comments
 (0)