Maui在IOS 13+中隐藏状态栏

avwztpqn  于 2023-05-19  发布在  iOS
关注(0)|答案(2)|浏览(253)

下面的代码可以工作,但已经过时了。我正在寻找更好的解决方案。

UIApplication.SharedApplication.SetStatusBarHidden(shouldBeFullScreen, animation: UIKit.UIStatusBarAnimation.Fade);

我需要使用UIViewController PrefersStatusBarHidden();
我试过:

UIWindow window = UIApplication.SharedApplication.Delegate.GetWindow();

        var vc = window?.RootViewController ?? WindowStateManager.Default.GetCurrentUIViewController() ?? throw new InvalidOperationException($"{nameof(window.RootViewController)} cannot be null");
        while (vc.PresentedViewController is not null)
        {
            vc = vc.PresentedViewController;
        }
            vc.PrefersStatusBarHidden();
            vc.SetNeedsStatusBarAppearanceUpdate();

上面的代码确实抓取了窗口,但实际上并没有隐藏状态栏。
我也试过:

Microsoft.Maui.Controls.PlatformConfiguration.iOSSpecific.Page.SetPrefersStatusBarHidden(currentPage, item);

任何建议将不胜感激。我正在为社区工具包的媒体元素中实现全屏控制,我被困在IOS中隐藏状态栏。

noj0wjuj

noj0wjuj1#

将以下行添加到info.plist文件中。右键点击info.plist,选择【打开方式】,然后选择【源代码】

<key>UIStatusBarHidden</key>
<true/>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
gpnt7bae

gpnt7bae2#

prefersStatusBarHidden是UIViewController的虚方法,需要在当前UIViewController中重写,才能更改状态栏。一种方法是为该页面创建一个新的UIViewController,但这意味着您必须以本机方式编写所有控件。这可能会引起更多的麻烦。

或者你可以参考这个线程:iOS PageRenderer compatibility not rendering page content和PureWeen评论道。下面是一个简单的演示:
在iOS文件夹中,您可以创建一个继承PageHandler的HideStatusBarPageHandler

public class HideStatusBarPageHandler : PageHandler
{
    class MainPageViewController : PageViewController
    {
        public bool IsStatusBarHidden { get; set; }

        public MainPageViewController(IView page, IMauiContext mauiContext) : base(page, mauiContext)
        {
        }
        
        // in this viewController, override the method which returns a variable that we could set true or false to it
        public override bool PrefersStatusBarHidden()
        {
            return IsStatusBarHidden;
        }
    }
    
    // the following code is from PureWeen commented with small changes
    protected override Microsoft.Maui.Platform.ContentView CreatePlatformView()
    {
        _ = VirtualView ?? throw new InvalidOperationException($"{nameof(VirtualView)} must be set to create a LayoutView");
        _ = MauiContext ?? throw new InvalidOperationException($"{nameof(MauiContext)} cannot be null");

        if (ViewController == null)
            ViewController = new MainPageViewController(VirtualView, MauiContext);

        if (ViewController is PageViewController pc && pc.CurrentPlatformView is Microsoft.Maui.Platform.ContentView pv)
            return pv;

        if (ViewController.View is Microsoft.Maui.Platform.ContentView cv)
            return cv;

        throw new InvalidOperationException($"PageViewController.View must be a {nameof(Microsoft.Maui.Platform.ContentView)}");
    }
}

在MauiProgram.cs中,添加处理程序:

builder.ConfigureMauiHandlers(handlers =>
    {
#if IOS
        handlers.AddHandler(typeof(MainPage),typeof(HideStatusBarPageHandler));
#endif
    });

然后,当您获得当前ViewControl时,您可以通过更改IsStatusBarHidden属性来控制状态栏的显示或隐藏。

vc.IsStatusBarHidden = !vc.IsStatusBarHidden;
vc.SetNeedsStatusBarAppearanceUpdate();

希望能成功。

相关问题