Skip to content

Commit

Permalink
Fix Snackbar layout #1901
Browse files Browse the repository at this point in the history
  • Loading branch information
VladislavAntonyuk committed Jan 19, 2025
1 parent e66f78e commit 3000b06
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@
</pages:BasePage.Resources>

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


<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."
LineBreakMode = "WordWrap" />
LineBreakMode = "WordWrap" />

<Label Text="Windows uses toast notifications to display snackbar. Make sure you switched off Focus Assist."
IsVisible="{OnPlatform Default='false', WinUI='true'}"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,25 @@ async void DisplayCustomSnackbarButtonClicked(object? sender, EventArgs args)
throw new NotSupportedException($"{nameof(DisplayCustomSnackbarButton)}.{nameof(ITextButton.Text)} Not Recognized");
}
}
async void DisplayCustomSnackbarButtonClicked2(object sender, EventArgs e)
{
var options = new SnackbarOptions
{
BackgroundColor = Colors.Red,
TextColor = Colors.Green,
CharacterSpacing = 1,
ActionButtonFont = Font.SystemFontOfSize(14),
ActionButtonTextColor = Colors.Yellow,
CornerRadius = new CornerRadius(10),
Font = Font.SystemFontOfSize(14),
};
await DisplayCustomSnackbarButton2.DisplaySnackbar(
"This is a customized Snackbar",
() => DisplayCustomSnackbarButton2.BackgroundColor = Colors.Blue,
"Close",
TimeSpan.FromSeconds(5),
options);
}

void Snackbar_Dismissed(object? sender, EventArgs e)
{
Expand Down
25 changes: 20 additions & 5 deletions src/CommunityToolkit.Maui.Core/Views/Alert/AlertView.macios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace CommunityToolkit.Maui.Core.Views;
/// </summary>
public class AlertView : UIView
{
const int defaultSpacing = 10;
readonly List<UIView> children = [];

/// <summary>
Expand Down Expand Up @@ -55,23 +56,37 @@ public void Setup()
ConstraintInParent();
}

void ConstraintInParent()
/// <inheritdoc />
public override void LayoutSubviews()
{
base.LayoutSubviews();
_ = Container ?? throw new InvalidOperationException($"{nameof(AlertView)}.{nameof(Initialize)} not called");

const int defaultSpacing = 10;
if (AnchorView is null)
{
this.SafeBottomAnchor().ConstraintEqualTo(ParentView.SafeBottomAnchor(), -defaultSpacing).Active = true;
this.SafeTopAnchor().ConstraintGreaterThanOrEqualTo(ParentView.SafeTopAnchor(), defaultSpacing).Active = true;
}
else
{
this.SafeBottomAnchor().ConstraintEqualTo(AnchorView.SafeTopAnchor(), -defaultSpacing).Active = true;
var anchorViewPosition = AnchorView.Superview.ConvertRectToView(AnchorView.Frame, null);

Check failure on line 72 in src/CommunityToolkit.Maui.Core/Views/Alert/AlertView.macios.cs

View workflow job for this annotation

GitHub Actions / Build Library (macos-15)

Dereference of a possibly null reference.

Check failure on line 72 in src/CommunityToolkit.Maui.Core/Views/Alert/AlertView.macios.cs

View workflow job for this annotation

GitHub Actions / Build Library (macos-15)

Dereference of a possibly null reference.

Check failure on line 72 in src/CommunityToolkit.Maui.Core/Views/Alert/AlertView.macios.cs

View workflow job for this annotation

GitHub Actions / Build Library (macos-15)

Dereference of a possibly null reference.

Check failure on line 72 in src/CommunityToolkit.Maui.Core/Views/Alert/AlertView.macios.cs

View workflow job for this annotation

GitHub Actions / Build Library (macos-15)

Dereference of a possibly null reference.

Check failure on line 72 in src/CommunityToolkit.Maui.Core/Views/Alert/AlertView.macios.cs

View workflow job for this annotation

GitHub Actions / Build Sample App using Latest .NET SDK (macos-15)

Dereference of a possibly null reference.

Check failure on line 72 in src/CommunityToolkit.Maui.Core/Views/Alert/AlertView.macios.cs

View workflow job for this annotation

GitHub Actions / Build Sample App using Latest .NET SDK (macos-15)

Dereference of a possibly null reference.

Check failure on line 72 in src/CommunityToolkit.Maui.Core/Views/Alert/AlertView.macios.cs

View workflow job for this annotation

GitHub Actions / Build Sample App using Latest .NET SDK (macos-15)

Dereference of a possibly null reference.

Check failure on line 72 in src/CommunityToolkit.Maui.Core/Views/Alert/AlertView.macios.cs

View workflow job for this annotation

GitHub Actions / Build Sample App using Latest .NET SDK (macos-15)

Dereference of a possibly null reference.
if (anchorViewPosition.Top < Container.Frame.Height + SafeAreaLayoutGuide.LayoutFrame.Bottom)
{
this.SafeTopAnchor().ConstraintEqualTo(AnchorView.SafeBottomAnchor(), defaultSpacing).Active = true;
}
else
{
this.SafeBottomAnchor().ConstraintEqualTo(AnchorView.SafeTopAnchor(), -defaultSpacing).Active = true;
}
}
}

void ConstraintInParent()
{
_ = Container ?? throw new InvalidOperationException($"{nameof(AlertView)}.{nameof(Initialize)} not called");

this.SafeLeadingAnchor().ConstraintGreaterThanOrEqualTo(ParentView.SafeLeadingAnchor(), defaultSpacing).Active = true;
this.SafeTrailingAnchor().ConstraintLessThanOrEqualTo(ParentView.SafeTrailingAnchor(), -defaultSpacing).Active = true;
this.SafeLeadingAnchor().ConstraintEqualTo(ParentView.SafeLeadingAnchor(), defaultSpacing).Active = true;
this.SafeTrailingAnchor().ConstraintEqualTo(ParentView.SafeTrailingAnchor(), -defaultSpacing).Active = true;
this.SafeCenterXAnchor().ConstraintEqualTo(ParentView.SafeCenterXAnchor()).Active = true;

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

0 comments on commit 3000b06

Please sign in to comment.