Skip to content

Commit 3000b06

Browse files
Fix Snackbar layout #1901
1 parent e66f78e commit 3000b06

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

samples/CommunityToolkit.Maui.Sample/Pages/Alerts/SnackbarPage.xaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@
1515
</pages:BasePage.Resources>
1616

1717
<VerticalStackLayout Spacing="12">
18+
<Button x:Name="DisplayCustomSnackbarButton2"
19+
Text="Display Custom Snackbar"
20+
Clicked="DisplayCustomSnackbarButtonClicked2"
21+
TextColor="{Binding Source={RelativeSource Self}, Path=BackgroundColor, Converter={StaticResource ColorToColorForTextConverter}, x:DataType=Button}"/>
22+
1823

1924
<Label Text="The Snackbar is a timed alert that appears at the bottom of the screen by default. It is dismissed after a configurable duration of time. Snackbar is fully customizable and can be anchored to any IView."
20-
LineBreakMode = "WordWrap" />
25+
LineBreakMode = "WordWrap" />
2126

2227
<Label Text="Windows uses toast notifications to display snackbar. Make sure you switched off Focus Assist."
2328
IsVisible="{OnPlatform Default='false', WinUI='true'}"/>

samples/CommunityToolkit.Maui.Sample/Pages/Alerts/SnackbarPage.xaml.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,25 @@ async void DisplayCustomSnackbarButtonClicked(object? sender, EventArgs args)
8282
throw new NotSupportedException($"{nameof(DisplayCustomSnackbarButton)}.{nameof(ITextButton.Text)} Not Recognized");
8383
}
8484
}
85+
async void DisplayCustomSnackbarButtonClicked2(object sender, EventArgs e)
86+
{
87+
var options = new SnackbarOptions
88+
{
89+
BackgroundColor = Colors.Red,
90+
TextColor = Colors.Green,
91+
CharacterSpacing = 1,
92+
ActionButtonFont = Font.SystemFontOfSize(14),
93+
ActionButtonTextColor = Colors.Yellow,
94+
CornerRadius = new CornerRadius(10),
95+
Font = Font.SystemFontOfSize(14),
96+
};
97+
await DisplayCustomSnackbarButton2.DisplaySnackbar(
98+
"This is a customized Snackbar",
99+
() => DisplayCustomSnackbarButton2.BackgroundColor = Colors.Blue,
100+
"Close",
101+
TimeSpan.FromSeconds(5),
102+
options);
103+
}
85104

86105
void Snackbar_Dismissed(object? sender, EventArgs e)
87106
{

src/CommunityToolkit.Maui.Core/Views/Alert/AlertView.macios.cs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace CommunityToolkit.Maui.Core.Views;
88
/// </summary>
99
public class AlertView : UIView
1010
{
11+
const int defaultSpacing = 10;
1112
readonly List<UIView> children = [];
1213

1314
/// <summary>
@@ -55,23 +56,37 @@ public void Setup()
5556
ConstraintInParent();
5657
}
5758

58-
void ConstraintInParent()
59+
/// <inheritdoc />
60+
public override void LayoutSubviews()
5961
{
62+
base.LayoutSubviews();
6063
_ = Container ?? throw new InvalidOperationException($"{nameof(AlertView)}.{nameof(Initialize)} not called");
6164

62-
const int defaultSpacing = 10;
6365
if (AnchorView is null)
6466
{
6567
this.SafeBottomAnchor().ConstraintEqualTo(ParentView.SafeBottomAnchor(), -defaultSpacing).Active = true;
6668
this.SafeTopAnchor().ConstraintGreaterThanOrEqualTo(ParentView.SafeTopAnchor(), defaultSpacing).Active = true;
6769
}
6870
else
6971
{
70-
this.SafeBottomAnchor().ConstraintEqualTo(AnchorView.SafeTopAnchor(), -defaultSpacing).Active = true;
72+
var anchorViewPosition = AnchorView.Superview.ConvertRectToView(AnchorView.Frame, null);
73+
if (anchorViewPosition.Top < Container.Frame.Height + SafeAreaLayoutGuide.LayoutFrame.Bottom)
74+
{
75+
this.SafeTopAnchor().ConstraintEqualTo(AnchorView.SafeBottomAnchor(), defaultSpacing).Active = true;
76+
}
77+
else
78+
{
79+
this.SafeBottomAnchor().ConstraintEqualTo(AnchorView.SafeTopAnchor(), -defaultSpacing).Active = true;
80+
}
7181
}
82+
}
83+
84+
void ConstraintInParent()
85+
{
86+
_ = Container ?? throw new InvalidOperationException($"{nameof(AlertView)}.{nameof(Initialize)} not called");
7287

73-
this.SafeLeadingAnchor().ConstraintGreaterThanOrEqualTo(ParentView.SafeLeadingAnchor(), defaultSpacing).Active = true;
74-
this.SafeTrailingAnchor().ConstraintLessThanOrEqualTo(ParentView.SafeTrailingAnchor(), -defaultSpacing).Active = true;
88+
this.SafeLeadingAnchor().ConstraintEqualTo(ParentView.SafeLeadingAnchor(), defaultSpacing).Active = true;
89+
this.SafeTrailingAnchor().ConstraintEqualTo(ParentView.SafeTrailingAnchor(), -defaultSpacing).Active = true;
7590
this.SafeCenterXAnchor().ConstraintEqualTo(ParentView.SafeCenterXAnchor()).Active = true;
7691

7792
Container.SafeLeadingAnchor().ConstraintEqualTo(this.SafeLeadingAnchor(), defaultSpacing).Active = true;

0 commit comments

Comments
 (0)