Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[iOS/Catalyst] Fix Shell TitleView rendering issues on iOS 16 #12834

Merged
merged 6 commits into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions src/Controls/samples/Controls.Sample/Pages/AppShell.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,36 @@
xmlns:pages="using:Maui.Controls.Sample.Pages"
xmlns:shellPages="clr-namespace:Maui.Controls.Sample.Pages.ShellGalleries"
FlyoutBackground="{AppThemeBinding Dark=Black, Light=White}"
Title="Welcome to Shell"
>
Title="{Binding ShellTitle}">
<Shell.TitleView>
<Grid
BackgroundColor="Red"
VerticalOptions="Center"
HeightRequest="44">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label
FontSize="Large"
FontAttributes="Bold"
HorizontalTextAlignment="Center"
VerticalTextAlignment="Center"
Text="TitleView" />
<HorizontalStackLayout
Grid.Column="1"
Padding="6">
<Label
FontSize="Small"
HorizontalTextAlignment="Center"
VerticalTextAlignment="Center"
Text="{Binding ShellTitle}"/>
<Button
Text="Change TabBar BackgroundColor"
Clicked="OnChangeTabBarBackgroundColor" />
</HorizontalStackLayout>
</Grid>
</Shell.TitleView>
<Shell.FlyoutHeader>
<Image Source="dotnet_bot.png"></Image>
</Shell.FlyoutHeader>
Expand Down
10 changes: 10 additions & 0 deletions src/Controls/samples/Controls.Sample/Pages/AppShell.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,18 @@ public partial class AppShell
public AppShell()
{
InitializeComponent();
BindingContext = this;
SetTabBarBackgroundColor(this, Color.FromRgba(3, 169, 244, 255));
}

public string ShellTitle { get; set; } = "Welcome to Shell";

void OnChangeTabBarBackgroundColor(object sender, EventArgs e)
{
var random = new Random();

SetTabBarBackgroundColor(this, Color.FromRgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255)));
}
}

public class PageSearchHandler : SearchHandler
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#nullable disable
#nullable disable
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
Expand Down Expand Up @@ -551,7 +551,7 @@ public override void WillMoveToSuperview(UIView newSuper)

void UpdateFrame(UIView newSuper)
{
if (newSuper != null)
if (newSuper is not null && newSuper.Bounds != CGRect.Empty)
{
if (!(OperatingSystem.IsIOSVersionAtLeast(11) || OperatingSystem.IsTvOSVersionAtLeast(11)))
Frame = new CGRect(Frame.X, newSuper.Bounds.Y, Frame.Width, newSuper.Bounds.Height);
Expand Down
35 changes: 31 additions & 4 deletions src/Controls/tests/DeviceTests/Elements/Shell/ShellTests.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CoreGraphics;
using Foundation;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Handlers.Compatibility;
using Microsoft.Maui.Controls.Platform;
using Microsoft.Maui.Controls.Platform.Compatibility;
using Microsoft.Maui.Controls.PlatformConfiguration;
using Microsoft.Maui.Controls.PlatformConfiguration.iOSSpecific;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Platform;
using UIKit;
using Xunit;
using UIModalPresentationStyle = Microsoft.Maui.Controls.PlatformConfiguration.iOSSpecific.UIModalPresentationStyle;
using CoreGraphics;

#if ANDROID || IOS || MACCATALYST
using ShellHandler = Microsoft.Maui.Controls.Handlers.Compatibility.ShellRenderer;
#endif

namespace Microsoft.Maui.DeviceTests
{
Expand Down Expand Up @@ -203,6 +201,35 @@ void ShellNavigating(object sender, ShellNavigatingEventArgs e)
});
}

[Fact(DisplayName = "TitleView renders correctly")]
public async Task TitleViewRendersCorrectly()
{
SetupBuilder();

var expected = Colors.Red;

var shellTitleView = new VerticalStackLayout { BackgroundColor = expected };
var titleViewContent = new Label { Text = "TitleView" };
shellTitleView.Children.Add(titleViewContent);

var shell = await CreateShellAsync(shell =>
{
Shell.SetTitleView(shell, shellTitleView);

shell.CurrentItem = new ContentPage();
});

await CreateHandlerAndAddToWindow<ShellHandler>(shell, async (handler) =>
{
await Task.Delay(100);
Assert.NotNull(shell.Handler);
var platformShellTitleView = shellTitleView.ToPlatform();
Assert.Equal(platformShellTitleView, GetTitleView(handler));
Assert.NotEqual(platformShellTitleView.Frame, CGRect.Empty);
Assert.Equal(platformShellTitleView.BackgroundColor.ToColor(), expected);
});
}

protected async Task OpenFlyout(ShellRenderer shellRenderer, TimeSpan? timeOut = null)
{
var flyoutView = GetFlyoutPlatformView(shellRenderer);
Expand Down