diff --git a/src/Controls/samples/Controls.Sample/Pages/Core/ContentPageGalleries/ContentPageBackgroundGallery.xaml b/src/Controls/samples/Controls.Sample/Pages/Core/ContentPageGalleries/ContentPageBackgroundGallery.xaml new file mode 100644 index 000000000000..e6fae7a347e7 --- /dev/null +++ b/src/Controls/samples/Controls.Sample/Pages/Core/ContentPageGalleries/ContentPageBackgroundGallery.xaml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/Controls/samples/Controls.Sample/Pages/Core/ContentPageGalleries/ContentPageBackgroundGallery.xaml.cs b/src/Controls/samples/Controls.Sample/Pages/Core/ContentPageGalleries/ContentPageBackgroundGallery.xaml.cs new file mode 100644 index 000000000000..072195b56e0c --- /dev/null +++ b/src/Controls/samples/Controls.Sample/Pages/Core/ContentPageGalleries/ContentPageBackgroundGallery.xaml.cs @@ -0,0 +1,10 @@ +namespace Maui.Controls.Sample.Pages +{ + public partial class ContentPageBackgroundGallery + { + public ContentPageBackgroundGallery() + { + InitializeComponent(); + } + } +} \ No newline at end of file diff --git a/src/Controls/samples/Controls.Sample/Pages/Core/ContentPageGallery.xaml b/src/Controls/samples/Controls.Sample/Pages/Core/ContentPageGalleries/ContentPageBackgroundImageGallery.xaml similarity index 61% rename from src/Controls/samples/Controls.Sample/Pages/Core/ContentPageGallery.xaml rename to src/Controls/samples/Controls.Sample/Pages/Core/ContentPageGalleries/ContentPageBackgroundImageGallery.xaml index edb658224e59..aade48a9b4f6 100644 --- a/src/Controls/samples/Controls.Sample/Pages/Core/ContentPageGallery.xaml +++ b/src/Controls/samples/Controls.Sample/Pages/Core/ContentPageGalleries/ContentPageBackgroundImageGallery.xaml @@ -1,16 +1,16 @@  \ No newline at end of file diff --git a/src/Controls/samples/Controls.Sample/Pages/Core/ContentPageGalleries/ContentPageBackgroundImageGallery.xaml.cs b/src/Controls/samples/Controls.Sample/Pages/Core/ContentPageGalleries/ContentPageBackgroundImageGallery.xaml.cs new file mode 100644 index 000000000000..a9708b358262 --- /dev/null +++ b/src/Controls/samples/Controls.Sample/Pages/Core/ContentPageGalleries/ContentPageBackgroundImageGallery.xaml.cs @@ -0,0 +1,10 @@ +namespace Maui.Controls.Sample.Pages +{ + public partial class ContentPageBackgroundImageGallery + { + public ContentPageBackgroundImageGallery() + { + InitializeComponent(); + } + } +} \ No newline at end of file diff --git a/src/Controls/samples/Controls.Sample/Pages/Core/ContentPageGalleries/ContentPageGallery.cs b/src/Controls/samples/Controls.Sample/Pages/Core/ContentPageGalleries/ContentPageGallery.cs new file mode 100644 index 000000000000..a7842ac01fad --- /dev/null +++ b/src/Controls/samples/Controls.Sample/Pages/Core/ContentPageGalleries/ContentPageGallery.cs @@ -0,0 +1,33 @@ +using Microsoft.Maui; +using Microsoft.Maui.Controls; +using Microsoft.Maui.Controls.Internals; + +namespace Maui.Controls.Sample.Pages +{ + [Preserve(AllMembers = true)] + public class ContentPageGallery : ContentPage + { + public ContentPageGallery() + { + var descriptionLabel = + new Label { Text = "ContentPage Galleries", Margin = new Thickness(2, 2, 2, 2) }; + + Title = "ContentPage Galleries"; + + Content = new ScrollView + { + Content = new StackLayout + { + Children = + { + descriptionLabel, + GalleryBuilder.NavButton("ContentPage BackgroundImage", () => + new ContentPageBackgroundImageGallery(), Navigation), + GalleryBuilder.NavButton("ContentPage Background", () => + new ContentPageBackgroundGallery(), Navigation), + } + } + }; + } + } +} \ No newline at end of file diff --git a/src/Controls/samples/Controls.Sample/Pages/Core/ContentPageGallery.xaml.cs b/src/Controls/samples/Controls.Sample/Pages/Core/ContentPageGallery.xaml.cs deleted file mode 100644 index affb63b96b1c..000000000000 --- a/src/Controls/samples/Controls.Sample/Pages/Core/ContentPageGallery.xaml.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace Maui.Controls.Sample.Pages -{ - public partial class ContentPageGallery - { - public ContentPageGallery() - { - InitializeComponent(); - } - } -} \ No newline at end of file diff --git a/src/Core/src/Platform/iOS/ViewExtensions.cs b/src/Core/src/Platform/iOS/ViewExtensions.cs index 0874ea676d9b..ef70664cac3d 100644 --- a/src/Core/src/Platform/iOS/ViewExtensions.cs +++ b/src/Core/src/Platform/iOS/ViewExtensions.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Numerics; using System.Threading.Tasks; +using CoreAnimation; using CoreGraphics; using Foundation; using Microsoft.Maui.Devices; @@ -264,16 +265,23 @@ public static void UpdateBackgroundLayerFrame(this UIView view) var layer = view.Layer; + UpdateBackgroundLayerFrame(layer, view.Bounds); + } + + static void UpdateBackgroundLayerFrame(CALayer layer, CGRect bounds) + { if (layer == null || layer.Sublayers == null || layer.Sublayers.Length == 0) return; - + foreach (var sublayer in layer.Sublayers) { - if (sublayer.Name == BackgroundLayerName && sublayer.Frame != view.Bounds) + if (sublayer.Name == BackgroundLayerName && sublayer.Frame != bounds) { - sublayer.Frame = view.Bounds; + sublayer.Frame = bounds; break; } + + UpdateBackgroundLayerFrame(sublayer, bounds); } } diff --git a/src/Core/tests/DeviceTests/Handlers/ContentView/ContentViewTests.iOS.cs b/src/Core/tests/DeviceTests/Handlers/ContentView/ContentViewTests.iOS.cs index 3afc3a80763c..70fb6de52381 100644 --- a/src/Core/tests/DeviceTests/Handlers/ContentView/ContentViewTests.iOS.cs +++ b/src/Core/tests/DeviceTests/Handlers/ContentView/ContentViewTests.iOS.cs @@ -1,15 +1,7 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection.Metadata; using System.Threading.Tasks; -using Microsoft.Maui.Controls; using Microsoft.Maui.DeviceTests.Stubs; -using Microsoft.Maui.Graphics; -using Microsoft.Maui.Handlers; -using ObjCRuntime; using UIKit; -using UserNotificationsUI; using Xunit; namespace Microsoft.Maui.DeviceTests.Handlers.ContentView @@ -17,6 +9,23 @@ namespace Microsoft.Maui.DeviceTests.Handlers.ContentView [Category(TestCategory.ContentView)] public partial class ContentViewTests { + [Theory(DisplayName = "Background Updates Correctly")] + [InlineData(0xFF0000)] + [InlineData(0x00FF00)] + [InlineData(0x0000FF)] + public async Task BackgroundUpdatesCorrectly(uint color) + { + var expected = Color.FromUint(color); + + var contentView = new ContentViewStub() + { + Content = new LabelStub { Text = "Background", TextColor = Colors.White }, + Background = new LinearGradientPaintStub(Colors.Red, Colors.Blue), + }; + + await ValidateHasColor(contentView, expected); + } + [Fact, Category(TestCategory.FlowDirection)] public async Task FlowDirectionPropagatesToContent() { @@ -114,5 +123,18 @@ public async Task DoesNotPropagateToContentWithExplicitFlowDirection() Assert.Equal(UIUserInterfaceLayoutDirection.LeftToRight, labelFlowDirection); } + + Platform.ContentView GetNativeContentView(ContentViewHandler contentViewHandler) => + contentViewHandler.PlatformView; + + Task ValidateHasColor(IContentView contentView, Color color, Action action = null) + { + return InvokeOnMainThreadAsync(() => + { + var nativeContentView = GetNativeContentView(CreateHandler(contentView)); + action?.Invoke(); + nativeContentView.AssertContainsColor(color); + }); + } } }