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

iOS中Admob插屏广告弹出时状态栏消失问题求助

解决AdMob插屏广告弹出后状态栏消失的问题

嘿,这个问题我之前帮不少开发者处理过,核心原因其实很明确:AdMob插屏广告展示时会自动创建一个全屏的临时视图控制器,它默认会隐藏状态栏;而你的应用没有导航控制器,系统没有默认的逻辑帮你恢复原来的状态栏设置,所以广告关闭后状态栏就没回来。给你几个针对性的解决办法:

1. 强制根视图控制器保持状态栏可见

在你的根视图控制器里重写状态栏相关的属性,把状态栏可见性固定为false,同时提供自然的更新动画:

override var prefersStatusBarHidden: Bool {
    // 强制状态栏始终可见
    return false
}

override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
    // 状态栏更新时用淡入淡出动画,避免突兀
    return .fade
}

然后在广告关闭的代理方法里,触发状态栏的外观更新,确保系统回到根控制器的设置:

func interstitialDidDismissScreen(_ ad: GADInterstitial) {
    // 调用根控制器的方法触发状态栏更新
    rootViewController?.setNeedsStatusBarAppearanceUpdate()
    // 别忘了重新加载下一个广告
    loadInterstitial()
}

2. 优化你的插屏广告控制器代码

针对你提供的InterstitialController,可以做一些调整,确保它关联正确的根视图控制器,并且在展示广告前就做好状态栏准备:

class InterstitialController: NSObject, GADInterstitialDelegate {
    static var shared = InterstitialController()
    // 明确关联根视图控制器,避免模糊的view属性
    var rootViewController: UIViewController?
    var interstitial: GADInterstitial!

    func loadInterstitial() {
        interstitial = GADInterstitial(adUnitID: "你的广告单元ID")
        interstitial.delegate = self
        interstitial.load(GADRequest())
    }

    func showInterstitial() {
        guard let rootVC = rootViewController, interstitial.isReady else {
            // 广告未就绪,重新加载
            loadInterstitial()
            return
        }
        // 展示前先触发一次状态栏更新,确保可见
        rootVC.setNeedsStatusBarAppearanceUpdate()
        interstitial.present(fromRootViewController: rootVC)
    }

    // 广告关闭后恢复状态栏
    func interstitialDidDismissScreen(_ ad: GADInterstitial) {
        rootViewController?.setNeedsStatusBarAppearanceUpdate()
        loadInterstitial()
    }

    // 广告加载失败时也要确保状态栏正常
    func interstitialDidFailToPresentScreen(_ ad: GADInterstitial) {
        rootViewController?.setNeedsStatusBarAppearanceUpdate()
    }
}

3. iOS 13+ 适配补充

如果你的应用适配iOS 13及以上,UIApplication.shared.isStatusBarHidden已经被弃用,可以改用窗口场景的状态栏管理器来强制设置:

// 在广告展示/关闭时调用
if let statusBarManager = UIApplication.shared.windows.first?.windowScene?.statusBarManager {
    statusBarManager.isStatusBarHidden = false
}

关键提醒

  • 一定要确保rootViewController指向的是应用的真正根视图控制器,而不是某个临时视图,否则状态栏的更新不会生效。
  • 测试时要覆盖广告加载失败、展示被中断等边缘场景,确保状态栏在任何情况下都能正常显示。

内容的提问来源于stack exchange,提问作者Pascal Friedrich

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:51:42