You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS 13+下Maui隐藏状态栏:弃用API替代方案求助

iOS下为Community Toolkit MediaElement实现全屏时隐藏状态栏的正确方案

问题背景

原有的状态栏隐藏代码已被弃用:

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

需要改用UIViewController.PrefersStatusBarHidden()相关逻辑,但尝试的两种方法均未生效:

  1. 获取顶层ViewController后调用PrefersStatusBarHidden()和SetNeedsStatusBarAppearanceUpdate()
  2. 使用MAUI iOS特定扩展Microsoft.Maui.Controls.PlatformConfiguration.iOSSpecific.Page.SetPrefersStatusBarHidden(currentPage, item);

当前需求是为Community Toolkit的MediaElement实现全屏控件,卡在iOS状态栏隐藏环节。


可行解决方案

方案1:MAUI原生配置方式(推荐,符合MAUI架构)

确保操作对象是当前显示的Page,通过PlatformConfiguration配置状态栏状态并触发更新:

using Microsoft.Maui.Controls.PlatformConfiguration;
using Microsoft.Maui.Controls.PlatformConfiguration.iOSSpecific;

// shouldBeFullScreen为全屏状态布尔值
currentPage.On<iOS>()
    .SetPrefersStatusBarHidden(shouldBeFullScreen ? StatusBarHiddenMode.Hidden : StatusBarHiddenMode.Visible)
    .SetPreferredStatusBarUpdateAnimation(UIStatusBarAnimation.Fade);
// 强制触发状态栏外观更新
currentPage.SetNeedsStatusBarAppearanceUpdate();

如果MediaElement处于NavigationPage中,建议同时控制导航栏显示:

if (shouldBeFullScreen)
{
    NavigationPage.SetHasNavigationBar(currentPage, false);
}
else
{
    NavigationPage.SetHasNavigationBar(currentPage, true);
}

方案2:自定义ViewController控制(适合渲染器场景)

针对MediaElement的全屏逻辑,自定义ViewController并重写状态栏相关属性:

  1. 创建全屏专用ViewController:
public class MediaFullScreenViewController : UIViewController
{
    public bool IsFullScreen { get; set; }

    // 重写状态栏隐藏状态逻辑
    public override bool PrefersStatusBarHidden()
    {
        return IsFullScreen;
    }

    // 设置状态栏切换动画
    public override UIStatusBarAnimation PreferredStatusBarUpdateAnimation()
    {
        return UIStatusBarAnimation.Fade;
    }
}
  1. 在MediaElement的iOS自定义渲染器中监听全屏状态变化:
protected override void OnElementChanged(ElementChangedEventArgs<MediaElement> e)
{
    base.OnElementChanged(e);
    if (e.NewElement != null)
    {
        e.NewElement.IsFullScreenChanged += OnIsFullScreenChanged;
    }
    if (e.OldElement != null)
    {
        e.OldElement.IsFullScreenChanged -= OnIsFullScreenChanged;
    }
}

private void OnIsFullScreenChanged(object sender, EventArgs e)
{
    var mediaElement = sender as MediaElement;
    // 根据Community Toolkit的MediaElement实现,获取对应的全屏ViewController
    var fullScreenVc = GetMediaFullScreenViewController();
    
    if (fullScreenVc != null)
    {
        fullScreenVc.IsFullScreen = mediaElement.IsFullScreen;
        fullScreenVc.SetNeedsStatusBarAppearanceUpdate();
    }
}

常见问题排查

  1. 获取正确的顶层ViewController:iOS 13+需通过UIApplication.SharedApplication.Windows.FirstOrDefault(w => w.IsKeyWindow)获取关键窗口,再遍历到最顶层的PresentedViewController。
  2. 不要直接调用PrefersStatusBarHidden():该方法是系统用来读取状态栏状态的,需重写其返回值或通过配置让系统获取正确状态。
  3. 必须触发更新:修改状态后务必调用SetNeedsStatusBarAppearanceUpdate(),否则系统不会更新状态栏外观。

内容的提问来源于stack exchange,提问作者James Crutchley

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 13:30:02