From 20cbe1ae813ff57364d66b8ea627fe3f23206b9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Su=C3=A1rez=20Ruiz?= Date: Fri, 10 Jun 2022 13:25:53 +0200 Subject: [PATCH] Fix issue not rendering Border background using a Shadow on iOS --- .../src/Handlers/Border/BorderHandler.iOS.cs | 19 +++++++++++ src/Core/src/Platform/iOS/ContentView.cs | 4 +++ src/Core/src/Platform/iOS/StrokeExtensions.cs | 34 +++++++++++++++++-- .../net6.0-ios/PublicAPI.Unshipped.txt | 2 ++ .../PublicAPI.Unshipped.txt | 2 ++ 5 files changed, 58 insertions(+), 3 deletions(-) diff --git a/src/Core/src/Handlers/Border/BorderHandler.iOS.cs b/src/Core/src/Handlers/Border/BorderHandler.iOS.cs index df9c4b11807f..a20f48163d3b 100644 --- a/src/Core/src/Handlers/Border/BorderHandler.iOS.cs +++ b/src/Core/src/Handlers/Border/BorderHandler.iOS.cs @@ -18,6 +18,20 @@ protected override ContentView CreatePlatformView() }; } + protected override void ConnectHandler(ContentView platformView) + { + base.ConnectHandler(platformView); + + platformView.LayoutSubviewsChanged += OnLayoutSubviewsChanged; + } + + protected override void DisconnectHandler(ContentView platformView) + { + base.DisconnectHandler(platformView); + + platformView.LayoutSubviewsChanged -= OnLayoutSubviewsChanged; + } + public override void SetVirtualView(IView view) { base.SetVirtualView(view); @@ -47,5 +61,10 @@ public static void MapContent(IBorderHandler handler, IBorderView border) { UpdateContent(handler); } + + void OnLayoutSubviewsChanged(object? sender, EventArgs e) + { + PlatformView?.UpdateMauiCALayer(); + } } } diff --git a/src/Core/src/Platform/iOS/ContentView.cs b/src/Core/src/Platform/iOS/ContentView.cs index 8e867638c2d1..0045b9e083bf 100644 --- a/src/Core/src/Platform/iOS/ContentView.cs +++ b/src/Core/src/Platform/iOS/ContentView.cs @@ -6,6 +6,8 @@ namespace Microsoft.Maui.Platform { public class ContentView : MauiView { + internal event EventHandler? LayoutSubviewsChanged; + public override CGSize SizeThatFits(CGSize size) { if (CrossPlatformMeasure == null) @@ -29,6 +31,8 @@ public override void LayoutSubviews() CrossPlatformMeasure?.Invoke(bounds.Width, bounds.Height); CrossPlatformArrange?.Invoke(bounds); + + LayoutSubviewsChanged?.Invoke(this, EventArgs.Empty); } public override void SetNeedsLayout() diff --git a/src/Core/src/Platform/iOS/StrokeExtensions.cs b/src/Core/src/Platform/iOS/StrokeExtensions.cs index a0d3bfcaaa29..578c91f00e7d 100644 --- a/src/Core/src/Platform/iOS/StrokeExtensions.cs +++ b/src/Core/src/Platform/iOS/StrokeExtensions.cs @@ -1,7 +1,7 @@ using System.Linq; using CoreAnimation; +using CoreGraphics; using Microsoft.Maui.Graphics; -using ObjCRuntime; using UIKit; namespace Microsoft.Maui.Platform @@ -125,21 +125,49 @@ internal static void UpdateMauiCALayer(this UIView platformView, IBorderStroke? if (backgroundLayer is MauiCALayer mauiCALayer) { backgroundLayer.Frame = platformView.Bounds; - if (border is IView v) - mauiCALayer.SetBackground(v.Background); + + if (border is IView view) + mauiCALayer.SetBackground(view.Background); else mauiCALayer.SetBackground(new SolidPaint(Colors.Transparent)); + mauiCALayer.SetBorderBrush(border?.Stroke); mauiCALayer.SetBorderWidth(border?.StrokeThickness ?? 0); mauiCALayer.SetBorderDash(border?.StrokeDashPattern, border?.StrokeDashOffset ?? 0); mauiCALayer.SetBorderMiterLimit(border?.StrokeMiterLimit ?? 0); + if (border != null) { mauiCALayer.SetBorderLineJoin(border.StrokeLineJoin); mauiCALayer.SetBorderLineCap(border.StrokeLineCap); } + mauiCALayer.SetBorderShape(border?.Shape); } } + + internal static void UpdateMauiCALayer(this UIView view) + { + if (view == null || view.Frame.IsEmpty) + return; + + var layer = view.Layer; + + UpdateBackgroundLayer(layer, view.Bounds); + } + + static void UpdateBackgroundLayer(this CALayer layer, CGRect bounds) + { + if (layer != null && layer.Sublayers != null) + { + foreach (var sublayer in layer.Sublayers) + { + UpdateBackgroundLayer(sublayer, bounds); + + if (sublayer.Name == ViewExtensions.BackgroundLayerName && sublayer.Frame != bounds) + sublayer.Frame = bounds; + } + } + } } } \ No newline at end of file diff --git a/src/Core/src/PublicAPI/net6.0-ios/PublicAPI.Unshipped.txt b/src/Core/src/PublicAPI/net6.0-ios/PublicAPI.Unshipped.txt index c65c5297b027..8171ebae2b1b 100644 --- a/src/Core/src/PublicAPI/net6.0-ios/PublicAPI.Unshipped.txt +++ b/src/Core/src/PublicAPI/net6.0-ios/PublicAPI.Unshipped.txt @@ -1,4 +1,6 @@ #nullable enable +override Microsoft.Maui.Handlers.BorderHandler.ConnectHandler(Microsoft.Maui.Platform.ContentView! platformView) -> void +override Microsoft.Maui.Handlers.BorderHandler.DisconnectHandler(Microsoft.Maui.Platform.ContentView! platformView) -> void override Microsoft.Maui.Platform.ContentView.SetNeedsLayout() -> void override Microsoft.Maui.Platform.LayoutView.HitTest(CoreGraphics.CGPoint point, UIKit.UIEvent? uievent) -> UIKit.UIView! override Microsoft.Maui.Platform.LayoutView.SetNeedsLayout() -> void diff --git a/src/Core/src/PublicAPI/net6.0-maccatalyst/PublicAPI.Unshipped.txt b/src/Core/src/PublicAPI/net6.0-maccatalyst/PublicAPI.Unshipped.txt index c65c5297b027..8171ebae2b1b 100644 --- a/src/Core/src/PublicAPI/net6.0-maccatalyst/PublicAPI.Unshipped.txt +++ b/src/Core/src/PublicAPI/net6.0-maccatalyst/PublicAPI.Unshipped.txt @@ -1,4 +1,6 @@ #nullable enable +override Microsoft.Maui.Handlers.BorderHandler.ConnectHandler(Microsoft.Maui.Platform.ContentView! platformView) -> void +override Microsoft.Maui.Handlers.BorderHandler.DisconnectHandler(Microsoft.Maui.Platform.ContentView! platformView) -> void override Microsoft.Maui.Platform.ContentView.SetNeedsLayout() -> void override Microsoft.Maui.Platform.LayoutView.HitTest(CoreGraphics.CGPoint point, UIKit.UIEvent? uievent) -> UIKit.UIView! override Microsoft.Maui.Platform.LayoutView.SetNeedsLayout() -> void