From 1a16cd6ef8eda8cd1330e45216acedf3a7d304fe Mon Sep 17 00:00:00 2001 From: cat0363 <125236133+cat0363@users.noreply.github.com> Date: Tue, 13 Jun 2023 22:39:37 +0900 Subject: [PATCH] Fix Issue #1160 (#1234) * Fixed code for Issue #1160 * Changed how to refer to Subviews * Revert and deprecate RoundedStackView --- .../Views/Alert/AlertView.macios.cs | 18 +++-- .../Views/RoundedStackView.macios.cs | 1 + .../Views/RoundedView.macios.cs | 78 +++++++++++++++++++ 3 files changed, 91 insertions(+), 6 deletions(-) create mode 100644 src/CommunityToolkit.Maui.Core/Views/RoundedView.macios.cs diff --git a/src/CommunityToolkit.Maui.Core/Views/Alert/AlertView.macios.cs b/src/CommunityToolkit.Maui.Core/Views/Alert/AlertView.macios.cs index ceca4f770..f4a81fb21 100644 --- a/src/CommunityToolkit.Maui.Core/Views/Alert/AlertView.macios.cs +++ b/src/CommunityToolkit.Maui.Core/Views/Alert/AlertView.macios.cs @@ -83,16 +83,11 @@ void ConstraintInParent() [MemberNotNull(nameof(Container))] void Initialize() { - Container = new RoundedStackView( - VisualOptions.CornerRadius.X, - VisualOptions.CornerRadius.Y, - VisualOptions.CornerRadius.Width, - VisualOptions.CornerRadius.Height) + Container = new UIStackView() { Alignment = UIStackViewAlignment.Fill, Distribution = UIStackViewDistribution.EqualSpacing, Axis = UILayoutConstraintAxis.Horizontal, - BackgroundColor = VisualOptions.BackgroundColor, TranslatesAutoresizingMaskIntoConstraints = false }; @@ -103,5 +98,16 @@ void Initialize() TranslatesAutoresizingMaskIntoConstraints = false; AddSubview(Container); + + var subView = new RoundedView( + VisualOptions.CornerRadius.X, + VisualOptions.CornerRadius.Y, + VisualOptions.CornerRadius.Width, + VisualOptions.CornerRadius.Height) + { + BackgroundColor = VisualOptions.BackgroundColor, + AutoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth + }; + Subviews[0].InsertSubview(subView, atIndex: 0); } } \ No newline at end of file diff --git a/src/CommunityToolkit.Maui.Core/Views/RoundedStackView.macios.cs b/src/CommunityToolkit.Maui.Core/Views/RoundedStackView.macios.cs index 50509c0b7..869578725 100644 --- a/src/CommunityToolkit.Maui.Core/Views/RoundedStackView.macios.cs +++ b/src/CommunityToolkit.Maui.Core/Views/RoundedStackView.macios.cs @@ -6,6 +6,7 @@ namespace CommunityToolkit.Maui.Core.Views; /// /// A rounded /// +[Obsolete] public class RoundedStackView : UIStackView { /// diff --git a/src/CommunityToolkit.Maui.Core/Views/RoundedView.macios.cs b/src/CommunityToolkit.Maui.Core/Views/RoundedView.macios.cs new file mode 100644 index 000000000..9a2e2f9c6 --- /dev/null +++ b/src/CommunityToolkit.Maui.Core/Views/RoundedView.macios.cs @@ -0,0 +1,78 @@ +using System.Runtime.InteropServices; +using CoreAnimation; + +namespace CommunityToolkit.Maui.Core.Views; + +/// +/// A rounded +/// +public class RoundedView : UIView +{ + /// + /// Initialize + /// + public RoundedView(NFloat leftPadding, NFloat topPadding, NFloat rightPadding, NFloat bottomPadding) + { + LeftPadding = leftPadding; + TopPadding = topPadding; + RightPadding = rightPadding; + BottomPadding = bottomPadding; + } + + /// + /// Left Padding + /// + public NFloat LeftPadding { get; } + + /// + /// Top Padding + /// + public NFloat TopPadding { get; } + + /// + /// Right Padding + /// + public NFloat RightPadding { get; } + + /// + /// Bottom Padding + /// + public NFloat BottomPadding { get; } + + /// + public override void Draw(CGRect rect) + { + ClipsToBounds = true; + + var path = GetRoundedPath(rect, LeftPadding, TopPadding, RightPadding, BottomPadding); + var maskLayer = new CAShapeLayer + { + Frame = rect, + Path = path + }; + + Layer.Mask = maskLayer; + Layer.MasksToBounds = true; + } + + static CGPath? GetRoundedPath(CGRect rect, NFloat left, NFloat top, NFloat right, NFloat bottom) + { + var path = new UIBezierPath(); + path.MoveTo(new CGPoint(rect.Width - right, rect.Y)); + + path.AddArc(new CGPoint(rect.X + rect.Width - right, rect.Y + right), right, (NFloat)(Math.PI * 1.5), (NFloat)Math.PI * 2, true); + path.AddLineTo(new CGPoint(rect.Width, rect.Height - bottom)); + + path.AddArc(new CGPoint(rect.X + rect.Width - bottom, rect.Y + rect.Height - bottom), bottom, 0, (NFloat)(Math.PI * .5), true); + path.AddLineTo(new CGPoint(left, rect.Height)); + + path.AddArc(new CGPoint(rect.X + left, rect.Y + rect.Height - left), left, (NFloat)(Math.PI * .5), (NFloat)Math.PI, true); + path.AddLineTo(new CGPoint(rect.X, top)); + + path.AddArc(new CGPoint(rect.X + top, rect.Y + top), top, (NFloat)Math.PI, (NFloat)(Math.PI * 1.5), true); + + path.ClosePath(); + + return path.CGPath; + } +} \ No newline at end of file