Skip to content

Commit 5a253ef

Browse files
Workaround for TopAppBar with animation on iOS
1 parent 53add0d commit 5a253ef

5 files changed

Lines changed: 47 additions & 8 deletions

File tree

samples/HorusStudio.Maui.MaterialDesignControls.Sample/Pages/TopAppBarPage.xaml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,24 @@
1010
x:TypeArguments="vm:TopAppBarViewModel"
1111
x:DataType="vm:TopAppBarViewModel"
1212
BackgroundColor="{AppThemeBinding Light={x:Static material:MaterialLightTheme.Surface}, Dark={x:Static material:MaterialDarkTheme.Surface}}">
13-
<views:ControlPageWithTopAppBar>
13+
<views:ControlPageWithTopAppBar
14+
AnimateScrollView="True">
1415
<views:ControlPageWithTopAppBar.PageContent>
1516
<VerticalStackLayout Spacing="8">
1617

1718
<material:MaterialCard
1819
Style="{StaticResource CardControlContainer}"
1920
Padding="24"
2021
Margin="0,24,0,0">
21-
<material:MaterialLabel
22-
Text="The MaterialTopAppBar above uses the ScrollViewName to bind the control to a ScrollView and run an animation when scrolling down."
23-
HorizontalOptions="Center"
24-
VerticalOptions="Center" />
22+
<VerticalStackLayout
23+
Spacing="16">
24+
<material:MaterialLabel
25+
Text="Top app bars display navigation, actions, and text at the top of a screen."
26+
Type="TitleMedium" />
27+
<material:MaterialLabel
28+
Text="The MaterialTopAppBar above uses the ScrollViewName to bind the control to a ScrollView and run an animation when scrolling down."
29+
Type="BodyMedium" />
30+
</VerticalStackLayout>
2531
</material:MaterialCard>
2632

2733
<material:MaterialLabel Style="{StaticResource SectionTitle}" Text="CenterAligned" />

samples/HorusStudio.Maui.MaterialDesignControls.Sample/ViewModels/TopAppBarViewModel.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ public partial class TopAppBarViewModel : BaseViewModel
2323

2424
public TopAppBarViewModel()
2525
{
26-
Subtitle = "Top app bars display navigation, actions, and text at the top of a screen.";
27-
2826
ChangeTrailingIcons();
2927
}
3028

samples/HorusStudio.Maui.MaterialDesignControls.Sample/Views/ControlPageWithTopAppBar.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
<Grid
1010
RowDefinitions="Auto,*">
1111
<material:MaterialTopAppBar
12+
x:Name="topAppBar"
1213
AutomationId="topAppBar"
1314
Headline="{Binding Title}"
1415
Description="{Binding Subtitle}"
1516
LeadingIconCommand="{Binding GoBackCommand}"
1617
LeadingIconIsBusy="{Binding Path=GoBackCommand.IsRunning}"
1718
LeadingIcon="ic_back.png"
1819
Type="Large"
19-
ScrollViewName="MdcControlPageScroll"
2020
TrailingIcons="{Binding ContextualActions}" />
2121
<ScrollView
2222
Grid.Row="1"

samples/HorusStudio.Maui.MaterialDesignControls.Sample/Views/ControlPageWithTopAppBar.xaml.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,26 @@ public partial class ControlPageWithTopAppBar : ContentView
1616
}
1717
});
1818

19+
public static readonly BindableProperty AnimateScrollViewProperty = BindableProperty.Create(nameof(AnimateScrollView), typeof(bool), typeof(ControlPageWithTopAppBar), false, propertyChanged: (bindable, _, newValue) =>
20+
{
21+
if (bindable is ControlPageWithTopAppBar self && newValue is bool animateScrollView)
22+
{
23+
self.topAppBar.ScrollViewName = animateScrollView ? "MdcControlPageScroll" : null;
24+
}
25+
});
26+
1927
public View? PageContent
2028
{
2129
get => (View?)GetValue(PageContentProperty);
2230
set => SetValue(PageContentProperty, value);
2331
}
2432

33+
public bool AnimateScrollView
34+
{
35+
get => (bool)GetValue(AnimateScrollViewProperty);
36+
set => SetValue(AnimateScrollViewProperty, value);
37+
}
38+
2539
public ControlPageWithTopAppBar()
2640
{
2741
InitializeComponent();

src/HorusStudio.Maui.MaterialDesignControls/Controls/TopAppBar/MaterialTopAppBar.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public enum MaterialTopAppBarType
5858
/// </example>
5959
/// <todoList>
6060
/// * [iOS] The scroll animation has lag by the headline size change
61+
/// * [iOS] When using <c>ScrollViewName</c> for collapse/expand animation and setting a <c>Description</c> that wraps to more than one line, layout issues may occur. It is recommended to set <see cref="DescriptionLineBreakMode">DescriptionLineBreakMode</see> to <see cref="LineBreakMode.TailTruncation">LineBreakMode.TailTruncation</see> in this scenario.
6162
/// </todoList>
6263
public class MaterialTopAppBar : Grid
6364
{
@@ -76,6 +77,7 @@ public class MaterialTopAppBar : Grid
7677
private static readonly BindableProperty.CreateDefaultValueDelegate DefaultDescriptionFontFamily = _ => MaterialFontFamily.Default;
7778
private const FontAttributes DefaultDescriptionFontAttributes = FontAttributes.None;
7879
private static readonly Thickness DefaultDescriptionMarginAdjustment = new(DescriptionLateralMargin, 8, DescriptionLateralMargin, 16);
80+
private static readonly LineBreakMode DefaultDescriptionLineBreakMode = LineBreakMode.WordWrap;
7981
private static readonly BindableProperty.CreateDefaultValueDelegate DefaultIconTintColorColor = _ => new AppThemeBindingExtension { Light = MaterialLightTheme.OnSurfaceVariant, Dark = MaterialDarkTheme.OnSurfaceVariant };
8082
private static readonly ImageSource? DefaultLeadingIcon = null;
8183
private static readonly ICommand? DefaultLeadingIconCommand = null;
@@ -183,6 +185,11 @@ public class MaterialTopAppBar : Grid
183185
/// The backing store for the <see cref="DescriptionMarginAdjustment">DescriptionMarginAdjustment</see> bindable property.
184186
/// </summary>
185187
public static readonly BindableProperty DescriptionMarginAdjustmentProperty = BindableProperty.Create(nameof(DescriptionMarginAdjustment), typeof(Thickness), typeof(MaterialTopAppBar), defaultValue: DefaultDescriptionMarginAdjustment, BindingMode.OneTime);
188+
189+
/// <summary>
190+
/// The backing store for the <see cref="DescriptionLineBreakMode">DescriptionLineBreakMode</see> bindable property.
191+
/// </summary>
192+
public static readonly BindableProperty DescriptionLineBreakModeProperty = BindableProperty.Create(nameof(DescriptionLineBreakMode), typeof(LineBreakMode), typeof(MaterialTopAppBar), defaultValue: DefaultDescriptionLineBreakMode, defaultBindingMode: BindingMode.OneTime);
186193

187194
/// <summary>
188195
/// The backing store for the <see cref="LeadingIcon">LeadingIcon</see> bindable property.
@@ -471,6 +478,19 @@ public Thickness DescriptionMarginAdjustment
471478
get => (Thickness)GetValue(DescriptionMarginAdjustmentProperty);
472479
set => SetValue(DescriptionMarginAdjustmentProperty, value);
473480
}
481+
482+
/// <summary>
483+
/// Gets or sets the line break mode for the description text of this top app bar.
484+
/// This is a bindable property.
485+
/// </summary>
486+
/// <default>
487+
/// <see cref="LineBreakMode.WordWrap">LineBreakMode.WordWrap</see>
488+
/// </default>
489+
public LineBreakMode DescriptionLineBreakMode
490+
{
491+
get => (LineBreakMode)GetValue(DescriptionLineBreakModeProperty);
492+
set => SetValue(DescriptionLineBreakModeProperty, value);
493+
}
474494

475495
/// <summary>
476496
/// Allows you to display an icon button on the left side of the top app bar.
@@ -751,6 +771,7 @@ private void CreateLayout()
751771
_descriptionLabel.SetBinding(MaterialLabel.FontFamilyProperty, new Binding(nameof(DescriptionFontFamily), source: this));
752772
_descriptionLabel.SetBinding(MaterialLabel.FontAttributesProperty, new Binding(nameof(DescriptionFontAttributes), source: this));
753773
_descriptionLabel.SetBinding(MaterialLabel.MarginProperty, new Binding(nameof(DescriptionMarginAdjustment), source: this));
774+
_descriptionLabel.SetBinding(MaterialLabel.LineBreakModeProperty, new Binding(nameof(DescriptionLineBreakMode), source: this));
754775
_descriptionLabel.SetBinding(MaterialLabel.AutomationIdProperty, new Binding(nameof(AutomationId), source: this, converter: new AutomationIdConverter(), converterParameter: "Description"));
755776
this.Add(_descriptionLabel, 0, 1);
756777
Grid.SetColumnSpan(_descriptionLabel, 3);

0 commit comments

Comments
 (0)