• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 在 iPhone 上切换应用时更改状态栏颜色的问题

[复制链接]
菜鸟教程小白 发表于 2022-12-13 03:16:53 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我想用特定的 View Controller 更改状态栏颜色。

根据 StackOverFlow 的回答,我做到了。

一个问题,在 iPhone 上切换应用时,我设置的颜色变淡,回到初始状态。

没关系。请注意状态栏。

enter image description here

不行。请注意状态栏。

enter image description here 我想不通。我试过的代码:

  1. 设置statusBar.backgroundColor,

    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey"statusBarWindow"] valueForKey"statusBar"];
    if ([statusBar respondsToSelectorselector(setBackgroundColor]) {
         statusBar.backgroundColor = [UIColor redColor ];
    }
    

2. 将 subview 插入状态栏。

 UIView *statusBar = [[[UIApplication sharedApplication] valueForKey"statusBarWindow"] valueForKey"statusBar"];
 UIView * backgroundColorView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 20) ];
 backgroundColorView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
 backgroundColorView.backgroundColor = [UIColor redColor ];
 [statusBar.subviews.firstObject insertSubview: backgroundColorView atIndex:0];

3.插入层(CALayer)也是如此。

我尝试用断点分析它。

- 应用处于事件状态时,双击Home键切换应用,方法不调用- (void)viewWillDisappearBOOL)animated 。这让我有点困惑。

-我尝试在Application的方法-(void)applicationWillResignActiveUIApplication *)application中更改状态栏的背景颜色,它不起作用。我不知道为什么。

虽然来自 Github 的源代码,通过 Runtime 就可以了。我的公司不喜欢使用 Runtime

还有其他方法无需运行时吗?

而且我不知道运行时如何与 iPhone 的切换应用程序模式交互。

主要问题是解决它无需运行时。欢迎更多解释。我认为这很容易,我想念什么?

非常感谢您的进步。



Best Answer-推荐答案


Swift 4 的答案:

并且适合navigationViewController管理的viewController的情况

class ViewController: UIViewController {

    let statusBarBgView = { () -> UIView in
        let statusBarWindow: UIView = UIApplication.shared.value(forKey: "statusBarWindow") as! UIView
        let statusBarBgView = UIView(frame: (statusBarWindow.statusBar?.bounds)!)
        return statusBarBgView
    }()


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        UIApplication.shared.statusBarStyle = .lightContent
        let navigationBar = self.navigationController?.navigationBar
        self.statusBarBgView.backgroundColor = UIColor.red
        navigationBar?.superview?.insertSubview(self.statusBarBgView, aboveSubview: navigationBar!)
    }


    override func viewWillDisappear(_ animated: Bool) {

        self.statusBarBgView.removeFromSuperview()
        super.viewWillDisappear(animated)
    }

}

extension UIView {
    var statusBar: UIView? {
        return value(forKey: "statusBar") as? UIView
    }
}

Objective-C 版本的答案:

-(void)viewWillAppearBOOL)animated{
    [super viewWillAppear: animated];
    [UIApplication sharedApplication].statusBarStyle=UIStatusBarStyleLightContent;
    UINavigationBar *navigationBar = self.navigationController.navigationBar;
    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey"statusBarWindow"] valueForKey"statusBar"];
    self.statusBarBgView = [[UIView alloc ] initWithFrame: statusBar.bounds ];
    self.statusBarBgView.backgroundColor = [UIColor redColor ];
    [navigationBar.superview insertSubview: self.statusBarBgView aboveSubview: navigationBar];
}

- (void)viewWillDisappearBOOL)animated {
    [self.statusBarBgView removeFromSuperview ];
    [super viewWillDisappear:animated];
}

显示操作系统信息的状态栏,是UIWindow,在应用切换窗口时由操作系统控制。

UIView *statusBar = [[[UIApplication sharedApplication] 
valueForKey"statusBarWindow"] valueForKey"statusBar"];

因此,在应用切换器窗口中,可以通过更改 View 来调整状态栏位置的背景颜色。

关于ios - 在 iPhone 上切换应用时更改状态栏颜色的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47250495/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap