Skip to content

Commit

Permalink
Fix Issue #1160 (#1234)
Browse files Browse the repository at this point in the history
* Fixed code for Issue #1160

* Changed how to refer to Subviews

* Revert and deprecate RoundedStackView
  • Loading branch information
cat0363 committed Jun 13, 2023
1 parent 312b794 commit 1a16cd6
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/CommunityToolkit.Maui.Core/Views/Alert/AlertView.macios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace CommunityToolkit.Maui.Core.Views;
/// <summary>
/// A rounded <see cref="UIStackView"/>
/// </summary>
[Obsolete]
public class RoundedStackView : UIStackView
{
/// <summary>
Expand Down
78 changes: 78 additions & 0 deletions src/CommunityToolkit.Maui.Core/Views/RoundedView.macios.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System.Runtime.InteropServices;
using CoreAnimation;

namespace CommunityToolkit.Maui.Core.Views;

/// <summary>
/// A rounded <see cref="UIView"/>
/// </summary>
public class RoundedView : UIView
{
/// <summary>
/// Initialize <see cref="RoundedView"/>
/// </summary>
public RoundedView(NFloat leftPadding, NFloat topPadding, NFloat rightPadding, NFloat bottomPadding)
{
LeftPadding = leftPadding;
TopPadding = topPadding;
RightPadding = rightPadding;
BottomPadding = bottomPadding;
}

/// <summary>
/// Left Padding
/// </summary>
public NFloat LeftPadding { get; }

/// <summary>
/// Top Padding
/// </summary>
public NFloat TopPadding { get; }

/// <summary>
/// Right Padding
/// </summary>
public NFloat RightPadding { get; }

/// <summary>
/// Bottom Padding
/// </summary>
public NFloat BottomPadding { get; }

/// <inheritdoc />
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;
}
}

0 comments on commit 1a16cd6

Please sign in to comment.