Skip to content

Commit ee5b86c

Browse files
Fix Snackbar layout #1901
1 parent e66f78e commit ee5b86c

File tree

5 files changed

+61
-11
lines changed

5 files changed

+61
-11
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/Alert.macios.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,12 @@ public void Dismiss()
6262
/// <summary>
6363
/// Show the <see cref="Alert"/> on the screen
6464
/// </summary>
65-
public void Show()
65+
/// <param name="stretch">Should stretch alert container horizontally</param>
66+
public void Show(bool stretch = false)
6667
{
6768
AlertView.AnchorView = Anchor;
6869

69-
AlertView.Setup();
70+
AlertView.Setup(stretch);
7071

7172
timer = NSTimer.CreateScheduledTimer(Duration, t =>
7273
{

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

Lines changed: 32 additions & 7 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>
@@ -49,29 +50,53 @@ public class AlertView : UIView
4950
/// <summary>
5051
/// Initializes <see cref="AlertView"/>
5152
/// </summary>
52-
public void Setup()
53+
/// <param name="stretch">Should stretch container horizontally</param>
54+
public void Setup(bool stretch = false)
5355
{
5456
Initialize();
55-
ConstraintInParent();
57+
ConstraintInParent(stretch);
5658
}
5759

58-
void ConstraintInParent()
60+
/// <inheritdoc />
61+
public override void LayoutSubviews()
5962
{
63+
base.LayoutSubviews();
6064
_ = Container ?? throw new InvalidOperationException($"{nameof(AlertView)}.{nameof(Initialize)} not called");
6165

62-
const int defaultSpacing = 10;
6366
if (AnchorView is null)
6467
{
6568
this.SafeBottomAnchor().ConstraintEqualTo(ParentView.SafeBottomAnchor(), -defaultSpacing).Active = true;
6669
this.SafeTopAnchor().ConstraintGreaterThanOrEqualTo(ParentView.SafeTopAnchor(), defaultSpacing).Active = true;
6770
}
6871
else
6972
{
70-
this.SafeBottomAnchor().ConstraintEqualTo(AnchorView.SafeTopAnchor(), -defaultSpacing).Active = true;
73+
var anchorViewPosition = AnchorView.Superview.ConvertRectToView(AnchorView.Frame, null);
74+
if (anchorViewPosition.Top < Container.Frame.Height + SafeAreaLayoutGuide.LayoutFrame.Bottom)
75+
{
76+
this.SafeTopAnchor().ConstraintEqualTo(AnchorView.SafeBottomAnchor(), defaultSpacing).Active = true;
77+
}
78+
else
79+
{
80+
this.SafeBottomAnchor().ConstraintEqualTo(AnchorView.SafeTopAnchor(), -defaultSpacing).Active = true;
81+
}
7182
}
83+
}
7284

73-
this.SafeLeadingAnchor().ConstraintGreaterThanOrEqualTo(ParentView.SafeLeadingAnchor(), defaultSpacing).Active = true;
74-
this.SafeTrailingAnchor().ConstraintLessThanOrEqualTo(ParentView.SafeTrailingAnchor(), -defaultSpacing).Active = true;
85+
void ConstraintInParent(bool stretch)
86+
{
87+
_ = Container ?? throw new InvalidOperationException($"{nameof(AlertView)}.{nameof(Initialize)} not called");
88+
89+
if (stretch)
90+
{
91+
this.SafeLeadingAnchor().ConstraintEqualTo(ParentView.SafeLeadingAnchor(), defaultSpacing).Active = true;
92+
this.SafeTrailingAnchor().ConstraintEqualTo(ParentView.SafeTrailingAnchor(), -defaultSpacing).Active = true;
93+
}
94+
else
95+
{
96+
this.SafeLeadingAnchor().ConstraintGreaterThanOrEqualTo(ParentView.SafeLeadingAnchor(), defaultSpacing).Active = true;
97+
this.SafeTrailingAnchor().ConstraintLessThanOrEqualTo(ParentView.SafeTrailingAnchor(), -defaultSpacing).Active = true;
98+
}
99+
75100
this.SafeCenterXAnchor().ConstraintEqualTo(ParentView.SafeCenterXAnchor()).Active = true;
76101

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

src/CommunityToolkit.Maui/Alerts/Snackbar/Snackbar.macios.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ async Task ShowPlatform(CancellationToken token)
7373
OnShown = OnShown
7474
};
7575

76-
PlatformSnackbar.Show();
76+
PlatformSnackbar.Show(true);
7777

7878
static T? GetMaximum<T>(params IReadOnlyList<T> items) => items.Max();
7979
}

0 commit comments

Comments
 (0)