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

iOS应用竖屏模式下状态栏固定在右侧的异常问题咨询

iOS竖屏应用状态栏卡在右侧?看这几个修复方案!

我之前也碰到过这个糟心的问题——竖屏专用的iOS app,旋转设备再切回来,状态栏就死死粘在右侧,只有退到主屏幕再打开才正常。先把问题重现步骤理清楚,再给你几个亲测有效的解决办法:

问题重现步骤

  • 启动仅支持竖屏的应用
  • 在切换应用/后台活动期间把设备转到横屏,此时app保持竖屏(这是正常的),但状态栏居然固定在右侧
  • 把设备转回竖屏,状态栏还是赖在右侧不动
  • 点Home键最小化app,状态栏终于回到顶部正常位置
  • 重新打开app,状态栏又跑偏到右侧

解决方案

这个问题本质是iOS系统没正确同步竖屏应用的状态栏方向,尤其是单一竖屏配置下,旋转设备后系统的状态栏状态没被正确重置。试试下面几种方法:

1. 强制更新状态栏方向(亲测最有效)

在你的根ViewController或者负责状态栏管理的VC里,重写viewWillAppear方法,每次页面出现时强制把状态栏拉回竖屏位置:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    // 强制状态栏回到竖屏顶部
    UIApplication.shared.setStatusBarOrientation(.portrait, animated: true)
}

// iOS13+还要加这个,确保状态栏偏好方向是竖屏
override var preferredStatusBarOrientation: UIInterfaceOrientation {
    return .portrait
}

Objective-C版本:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];
}

- (UIInterfaceOrientation)preferredStatusBarOrientation {
    return UIInterfaceOrientationPortrait;
}

2. 严格配置应用的支持方向

先检查你的Info.plist,确保只开了竖屏选项:

  • 打开Info.plist,找到Supported interface orientations
  • 只保留Portrait (bottom home button)这一项,把其他横屏、倒竖屏选项都删掉

这样能减少系统处理旋转时的混淆,从根源上降低状态栏跑偏的概率。

3. 监听旋转通知,动态修正

如果上面的方法不管用,那就监听设备旋转的通知,一旦切回竖屏就强制修正状态栏:

override func viewDidLoad() {
    super.viewDidLoad()
    // 监听设备旋转通知
    NotificationCenter.default.addObserver(self, selector: #selector(fixStatusBarOrientation), name: UIDevice.orientationDidChangeNotification, object: nil)
}

@objc func fixStatusBarOrientation() {
    // 设备回到竖屏时,强制设置状态栏方向
    let currentOrientation = UIDevice.current.orientation
    if currentOrientation == .portrait || currentOrientation == .portraitUpsideDown {
        UIApplication.shared.setStatusBarOrientation(.portrait, animated: true)
    }
}

// 记得销毁页面时移除通知,避免内存泄漏
deinit {
    NotificationCenter.default.removeObserver(self)
}

问题根源说明

当app只支持竖屏时,系统把设备转到横屏时,虽然不会旋转app界面,但可能会错误地保留横屏状态下的状态栏位置。通过强制设置状态栏为竖屏方向,就能让系统重新同步状态栏的正确位置,解决这个bug。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 06:58:19