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

Fix Issue #1193 #1248

Merged
merged 4 commits into from
Jun 15, 2023
Merged

Fix Issue #1193 #1248

merged 4 commits into from
Jun 15, 2023

Conversation

cat0363
Copy link
Contributor

@cat0363 cat0363 commented Jun 15, 2023

Using StatusBarBehavior changes the color of the status bar, but the size of the status bar does
not change when the screen is rotated. This PR fixes an issue where the size of the status bar
is not updated when the screen is rotated.

Description of Change

To solve this problem, I monitored the Page's SizeChanged event and updated the size of the
StatusBar when the screen was rotated. OnAttachedTo of StatusBarBeahvior registers the
SizeChanged event, and OnDetachedFrom unregisters the SizeChanged event. I modified it to
call the newly added UpdateBarSize method within the Page's SizeChanged event.

[StatusBarBehavior.shared.cs]

#if IOS
protected override void OnAttachedTo(Page bindable, UIKit.UIView platformView)
#elif ANDROID
protected override void OnAttachedTo(Page bindable, Android.Views.View platformView)
#else
protected override void OnAttachedTo(Page bindable, object platformView)
#endif
{
    StatusBar.SetColor(StatusBarColor);
    StatusBar.SetStyle(StatusBarStyle);
#if IOS
    bindable.SizeChanged += new EventHandler(page_SizeChanged);
#endif
}

#if IOS
protected override void OnDetachedFrom(Page bindable, UIKit.UIView platformView)
#elif ANDROID
protected override void OnDetachedFrom(Page bindable, Android.Views.View platformView)
#else
protected override void OnDetachedFrom(Page bindable, object platformView)
#endif
{
#if IOS
    bindable.SizeChanged -= new EventHandler(page_SizeChanged);
#endif
}

#if IOS
void page_SizeChanged(object? sender, EventArgs e)
{
    StatusBar.UpdateBarSize();
}
#endif

I defined the UpdateBarSize method for Shared and the PlatformUpdateBarSize method for each platform.

[StatusBar.shared.cs]

/// <summary>
/// Method to update the status bar size.
/// </summary>
public static void UpdateBarSize() =>
    PlatformUpdateBarSize();

[StatusBar.ios.cs]

static void PlatformUpdateBarSize()
{
    if (OperatingSystem.IsIOSVersionAtLeast(13))
    {
        var statusBarTag = new IntPtr(38482);
        foreach (var window in UIApplication.SharedApplication.Windows)
        {
            var statusBar = window.ViewWithTag(statusBarTag);
            var statusBarFrame = window.WindowScene?.StatusBarManager?.StatusBarFrame;
            if (statusBarFrame is null)
            {
                continue;
            }

            statusBar ??= new UIView(statusBarFrame.Value);
            statusBar.Tag = statusBarTag;
            statusBar.Frame = UIApplication.SharedApplication.StatusBarFrame;
            var statusBarSubViews = window.Subviews.Where(x => x.Tag == statusBarTag).ToList();
            foreach (var statusBarSubView in statusBarSubViews)
            {
                statusBarSubView.RemoveFromSuperview();
            }
            window.AddSubview(statusBar);

            UpdateStatusBarAppearance(window);
        }
    }
    else
    {
        if (UIApplication.SharedApplication.ValueForKey(new NSString("statusBar")) is UIView statusBar)
        {
            statusBar.Frame = UIApplication.SharedApplication.StatusBarFrame;
        }
        UpdateStatusBarAppearance();
    }
}

[StatusBar.android.cs]

static void PlatformUpdateBarSize()
{
    throw new NotSupportedException("Android does not currently support updating the status bar size");
}

[StatusBar.macos.cs]

static void PlatformUpdateBarSize()
{
    throw new NotSupportedException("MacCatalyst does not currently support updating the macOS status bar size");
}

[StatusBar.net.cs]

static void PlatformUpdateBarSize() => throw new NotSupportedException($"{nameof(PlatformUpdateBarSize)} is only supported on net6.0-ios and later");

[StatusBar.tizen.cs]

static void PlatformUpdateBarSize()
{
    throw new NotSupportedException("Tizen does not currently support updating the status bar size");
}

[StatusBar.windows.cs]

static void PlatformUpdateBarSize()
{
    throw new NotSupportedException("WinUI does not currently support updating the Windows status bar size");
}

Platforms other than iOS are outside the scope of this issue, so I implemented it to throw a NotSupportException.

Since I do not have a simulator prior to iOS 13 in my execution environment,
I have not been able to confirm the operation on versions prior to iOS 13.
Is there anyone who can verify that the following works?

if (UIApplication.SharedApplication.ValueForKey(new NSString("statusBar")) is UIView statusBar)
{
    statusBar.Frame = UIApplication.SharedApplication.StatusBarFrame;
}
UpdateStatusBarAppearance();

Linked Issues

PR Checklist

Additional information

Below is the execution result.

[iOS 16.4]

iPad.10th.generation.iOS.16.4.2023-06-15.13-01-13.mp4

[iOS 15.5]

iPad.9th.generation.iOS.15.5.2023-06-15.13-18-40.mp4

[iOS 14.5]

iPad.8th.generation.iOS.14.5.2023-06-15.13-58-52.mp4

[iOS 13.7]

iPad.7th.generation.iOS.13.7.2023-06-15.14-39-01.mp4

The video is a recording of the following steps.

  1. Perform a rotation operation
  2. Page transition
  3. Change status bar color
  4. Return to transition source page
  5. Perform a rotation operation

The color of the status bar is changed at both the page transition source and the page transition
destination, but the finally changed status bar color is retained, and only the size of the status bar is
changed after the rotation operation.

Removal of unnecessary methods for each platform other than iOS
@VladislavAntonyuk VladislavAntonyuk enabled auto-merge (squash) June 15, 2023 12:26
@VladislavAntonyuk VladislavAntonyuk merged commit 8e67d0a into CommunityToolkit:main Jun 15, 2023
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Using StatusBarBehavior does not display the status bar as intended after screen rotation on iOS.
2 participants