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

Commit 9e5a60b

Browse files
andreinitescujfversluis
authored andcommitted
Modal with transparent background
1 parent 6f41475 commit 9e5a60b

File tree

11 files changed

+173
-7
lines changed

11 files changed

+173
-7
lines changed

Xamarin.Forms.Controls/CoreGallery.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ public override string ToString()
417417
new GalleryPageFactory(() => new ViewCellGallery(), "ViewCell Gallery - Legacy"),
418418
new GalleryPageFactory(() => new WebViewGallery(), "WebView Gallery - Legacy"),
419419
new GalleryPageFactory(() => new BindableLayoutGalleryPage(), "BindableLayout Gallery - Legacy"),
420+
new GalleryPageFactory(() => new ShowModalWithTransparentBkgndGalleryPage(), "Modal With Transparent Bkgnd Gallery - Legacy"),
420421
};
421422

422423
public CorePageView(Page rootPage, NavigationBehavior navigationBehavior = NavigationBehavior.PushAsync)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
5+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
mc:Ignorable="d"
7+
x:Class="Xamarin.Forms.Controls.GalleryPages.PageWithTransparentBkgnd"
8+
xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
9+
ios:Page.UseSafeArea="true"
10+
BackgroundColor="Transparent"
11+
ModalBackgroundColor="#700000FF"
12+
ios:Page.ModalPresentationStyle="OverFullScreen">
13+
<Frame VerticalOptions="Center"
14+
HorizontalOptions="Center"
15+
WidthRequest="300"
16+
HeightRequest="200"
17+
Padding="15, 10"
18+
BackgroundColor="LightGreen">
19+
<StackLayout>
20+
<Label Text="Modal page with custom modal background"
21+
HorizontalOptions="Center"
22+
VerticalOptions="CenterAndExpand" />
23+
<Button Text="Close"
24+
HorizontalOptions="End"
25+
Clicked="ClosePageButtonClicked"
26+
VerticalOptions="End" />
27+
</StackLayout>
28+
</Frame>
29+
</ContentPage>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using Xamarin.Forms.Internals;
3+
4+
namespace Xamarin.Forms.Controls.GalleryPages
5+
{
6+
[Preserve(AllMembers = true)]
7+
public partial class PageWithTransparentBkgnd : ContentPage
8+
{
9+
public PageWithTransparentBkgnd()
10+
{
11+
InitializeComponent();
12+
}
13+
14+
void ClosePageButtonClicked(object sender, EventArgs e)
15+
{
16+
Navigation.PopModalAsync();
17+
}
18+
}
19+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using Xamarin.Forms.Internals;
2+
3+
namespace Xamarin.Forms.Controls.GalleryPages
4+
{
5+
class ShowModalWithTransparentBkgndGalleryPage : ContentPage
6+
{
7+
public ShowModalWithTransparentBkgndGalleryPage()
8+
{
9+
BackgroundColor = Color.LightPink;
10+
11+
var btn = new Button()
12+
{
13+
Text = "Show page",
14+
VerticalOptions = LayoutOptions.Start,
15+
HorizontalOptions = LayoutOptions.Center
16+
};
17+
18+
btn.Clicked += ShowModalBtnClicked;
19+
20+
Content = btn;
21+
}
22+
23+
void ShowModalBtnClicked(object sender, System.EventArgs e)
24+
{
25+
Navigation.PushModalAsync(new PageWithTransparentBkgnd());
26+
}
27+
28+
protected override void OnAppearing()
29+
{
30+
base.OnAppearing();
31+
}
32+
33+
protected override void OnDisappearing()
34+
{
35+
base.OnDisappearing();
36+
}
37+
}
38+
}

Xamarin.Forms.Controls/Xamarin.Forms.Controls.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@
9292
<EmbeddedResource Update="GalleryPages\RefreshViewGalleries\RefreshWebViewGallery.xaml">
9393
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
9494
</EmbeddedResource>
95+
<EmbeddedResource Update="GalleryPages\PageWithTransparentBkgnd.xaml">
96+
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
97+
</EmbeddedResource>
9598
<EmbeddedResource Update="GalleryPages\TitleView.xaml">
9699
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
97100
</EmbeddedResource>

Xamarin.Forms.Core/Page.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public class Page : VisualElement, ILayout, IPageController, IElementConfigurati
4242
[EditorBrowsable(EditorBrowsableState.Never)]
4343
public static readonly BindableProperty IconProperty = IconImageSourceProperty;
4444

45+
public static readonly BindableProperty ModalBackgroundColorProperty = BindableProperty.Create(nameof(ModalBackgroundColor), typeof(Color), typeof(Page), Color.Default);
46+
4547
readonly Lazy<PlatformConfigurationRegistry<Page>> _platformConfigurationRegistry;
4648

4749
bool _allocatedFlag;
@@ -153,6 +155,12 @@ public bool IgnoresContainerArea
153155
[EditorBrowsable(EditorBrowsableState.Never)]
154156
public ObservableCollection<Element> InternalChildren { get; } = new ObservableCollection<Element>();
155157

158+
public Color ModalBackgroundColor
159+
{
160+
get { return (Color)GetValue(ModalBackgroundColorProperty); }
161+
set { SetValue(ModalBackgroundColorProperty, value); }
162+
}
163+
156164
internal override IEnumerable<Element> ChildrenNotDrawnByThisElement
157165
{
158166
get

Xamarin.Forms.Core/PlatformConfiguration/iOSSpecific/UIModalPresentationStyle.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
public enum UIModalPresentationStyle
44
{
55
FullScreen,
6-
FormSheet
6+
FormSheet,
7+
OverFullScreen
78
}
89
}

Xamarin.Forms.Platform.Android/AppCompat/Platform.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Android.Views.Animations;
99
using AView = Android.Views.View;
1010
using Xamarin.Forms.Internals;
11+
using System.ComponentModel;
1112

1213
namespace Xamarin.Forms.Platform.Android.AppCompat
1314
{
@@ -429,7 +430,7 @@ public ModalContainer(Context context, Page modal) : base(context)
429430
_modal = modal;
430431

431432
_backgroundView = new AView(context);
432-
_backgroundView.SetWindowBackground();
433+
UpdateBackgroundColor();
433434
AddView(_backgroundView);
434435

435436
_renderer = Android.Platform.CreateRenderer(modal, context);
@@ -438,6 +439,8 @@ public ModalContainer(Context context, Page modal) : base(context)
438439
AddView(_renderer.View);
439440

440441
Id = Platform.GenerateViewId();
442+
443+
_modal.PropertyChanged += OnModalPagePropertyChanged;
441444
}
442445

443446
protected override void Dispose(bool disposing)
@@ -453,6 +456,7 @@ protected override void Dispose(bool disposing)
453456
_renderer.Dispose();
454457
_renderer = null;
455458
_modal.ClearValue(Android.Platform.RendererProperty);
459+
_modal.PropertyChanged -= OnModalPagePropertyChanged;
456460
_modal = null;
457461
}
458462

@@ -477,6 +481,21 @@ protected override void OnLayout(bool changed, int l, int t, int r, int b)
477481

478482
_renderer.UpdateLayout();
479483
}
484+
485+
void OnModalPagePropertyChanged(object sender, PropertyChangedEventArgs e)
486+
{
487+
if (e.PropertyName == Page.ModalBackgroundColorProperty.PropertyName)
488+
UpdateBackgroundColor();
489+
}
490+
491+
void UpdateBackgroundColor()
492+
{
493+
Color modalBkgndColor = _modal.ModalBackgroundColor;
494+
if (modalBkgndColor.IsDefault)
495+
_backgroundView.SetWindowBackground();
496+
else
497+
_backgroundView.SetBackgroundColor(modalBkgndColor.ToAndroid());
498+
}
480499
}
481500

482501
internal static int GenerateViewId()

Xamarin.Forms.Platform.iOS/Extensions/Extensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,16 @@ public static void ApplyKeyboard(this IUITextInputTraits textInput, Keyboard key
7676
}
7777
}
7878

79-
internal static UIModalPresentationStyle ToNativeModalPresentationStyle(this PlatformConfiguration.iOSSpecific.UIModalPresentationStyle style)
79+
public static UIModalPresentationStyle ToNativeModalPresentationStyle(this PlatformConfiguration.iOSSpecific.UIModalPresentationStyle style)
8080
{
8181
switch (style)
8282
{
8383
case PlatformConfiguration.iOSSpecific.UIModalPresentationStyle.FormSheet:
8484
return UIModalPresentationStyle.FormSheet;
8585
case PlatformConfiguration.iOSSpecific.UIModalPresentationStyle.FullScreen:
8686
return UIModalPresentationStyle.FullScreen;
87+
case PlatformConfiguration.iOSSpecific.UIModalPresentationStyle.OverFullScreen:
88+
return UIModalPresentationStyle.OverFullScreen;
8789
default:
8890
throw new ArgumentOutOfRangeException(nameof(style));
8991
}

Xamarin.Forms.Platform.iOS/ModalWrapper.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Linq;
33
using Xamarin.Forms.PlatformConfiguration.iOSSpecific;
44
using UIKit;
5+
using System.ComponentModel;
56

67
namespace Xamarin.Forms.Platform.iOS
78
{
@@ -14,15 +15,16 @@ internal ModalWrapper(IVisualElementRenderer modal)
1415
_modal = modal;
1516

1617
var elementConfiguration = modal.Element as IElementConfiguration<Page>;
17-
var modalPresentationStyle = elementConfiguration?.On<PlatformConfiguration.iOS>()?.ModalPresentationStyle() ?? PlatformConfiguration.iOSSpecific.UIModalPresentationStyle.FullScreen;
18-
ModalPresentationStyle = modalPresentationStyle.ToNativeModalPresentationStyle();
18+
if (elementConfiguration?.On<PlatformConfiguration.iOS>()?.ModalPresentationStyle() is PlatformConfiguration.iOSSpecific.UIModalPresentationStyle style)
19+
ModalPresentationStyle = style.ToNativeModalPresentationStyle();
1920

20-
View.BackgroundColor = UIColor.White;
21+
UpdateBackgroundColor();
2122
View.AddSubview(modal.ViewController.View);
2223
TransitioningDelegate = modal.ViewController.TransitioningDelegate;
2324
AddChildViewController(modal.ViewController);
2425

2526
modal.ViewController.DidMoveToParentViewController(this);
27+
((Page)modal.Element).PropertyChanged += OnModalPagePropertyChanged;
2628
}
2729

2830
public override void DismissViewController(bool animated, Action completionHandler)
@@ -89,14 +91,20 @@ public override void ViewDidLayoutSubviews()
8991

9092
public override void ViewWillAppear(bool animated)
9193
{
92-
View.BackgroundColor = UIColor.White;
94+
UpdateBackgroundColor();
9395
base.ViewWillAppear(animated);
9496
}
9597

9698
protected override void Dispose(bool disposing)
9799
{
98100
if (disposing)
101+
{
102+
if (_modal?.Element is Page modalPage)
103+
modalPage.PropertyChanged -= OnModalPagePropertyChanged;
104+
99105
_modal = null;
106+
}
107+
100108
base.Dispose(disposing);
101109
}
102110

@@ -112,5 +120,17 @@ public override UIViewController ChildViewControllerForStatusBarStyle()
112120
{
113121
return ChildViewControllers?.LastOrDefault();
114122
}
123+
124+
void OnModalPagePropertyChanged(object sender, PropertyChangedEventArgs e)
125+
{
126+
if (e.PropertyName == Page.ModalBackgroundColorProperty.PropertyName)
127+
UpdateBackgroundColor();
128+
}
129+
130+
void UpdateBackgroundColor()
131+
{
132+
Color modalBkgndColor = ((Page)_modal.Element).ModalBackgroundColor;
133+
View.BackgroundColor = modalBkgndColor.IsDefault ? UIColor.White : modalBkgndColor.ToUIColor();
134+
}
115135
}
116136
}

0 commit comments

Comments
 (0)