iOS 7 及更高版本:为每个视图控制器设置状态栏样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19013975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:42:28  来源:igfitidea点击:

iOS 7 and later: set status bar style per view controller

ioscocoa-touch

提问by Van Du Tran


I tried many ways to set the status bar style (default or lightcontent) but can't get it to work on a per view controller basis. I can set the status bar style for the whole app only.


我尝试了很多方法来设置状态栏样式(默认或 lightcontent),但无法让它在每个视图控制器的基础上工作。我只能为整个应用程序设置状态栏样式。

Does anyone have a hint?

有人有提示吗?

I tried UIViewControllerBasedStatusBarAppearance

我试过 UIViewControllerBasedStatusBarAppearance

and

-(UIStatusBarStyle)preferredStatusBarStyle{ 
    return UIStatusBarStyleLightContent; 
}

but these methods don't work.

但这些方法不起作用。

回答by Greg

Have you tried this?

你试过这个吗?

  1. Set "View controller-based status bar appearance" (UIViewControllerBasedStatusBarAppearance) to YESin your Info.plist. (YESis the default, so you can also just leave this value out of your plist.)

  2. In your viewDidLoad method, call [self setNeedsStatusBarAppearanceUpdate].

  3. Implement preferredStatusBarStyle, returning the status bar style that you want for this view controller.

    - (UIStatusBarStyle) preferredStatusBarStyle { 
        return UIStatusBarStyleLightContent; 
    }
    
  1. 在 Info.plist 中将“查看基于控制器的状态栏外观”( UIViewControllerBasedStatusBarAppearance) 设置为YES。(YES是默认值,因此您也可以将此值从 plist 中删除。)

  2. 在您的 viewDidLoad 方法中,调用[self setNeedsStatusBarAppearanceUpdate].

  3. 实现preferredStatusBarStyle,返回此视图控制器所需的状态栏样式。

    - (UIStatusBarStyle) preferredStatusBarStyle { 
        return UIStatusBarStyleLightContent; 
    }
    

回答by vishal dharankar

There is a catch here if your view controller is inside standalone UINavigationController and not a part of Storyboard based UINavigationController then above all methods fail. I came across this situation and then in order to set the status bar to light style i used following

如果您的视图控制器位于独立的 UINavigationController 内部而不是基于 Storyboard 的 UINavigationController 的一部分,那么这里有一个问题,那么最重要的是所有方法都会失败。我遇到了这种情况,然后为了将状态栏设置为浅色样式,我使用了以下

[self.navigationController.navigationBar setBarStyle:UIBarStyleBlack];

This worked perfectly for me.

这对我来说非常有效。

回答by caulitomaz

EDIT: This solution is deprecated on iOS 9. Please choose one of the other answers.

编辑:此解决方案在 iOS 9 上已弃用。请选择其他答案之一。

With UIViewControllerBasedStatusBarAppearanceset to NO, I was able to set the style to white text by using:

随着UIViewControllerBasedStatusBarAppearance设置为NO,我能够通过设置样式为白色文字:

[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackTranslucent;

This is because the text color on this style was white on iOS 6 and below.

这是因为此样式的文本颜色在 iOS 6 及以下版本中为白色。

UPDATE:According to @jowie you can try that on iOS8:

更新:根据@jowie,您可以在 iOS8 上尝试:

[UIApplication sharedApplication].statusBarStyle = UIBarStyleBlack;

回答by Fengson

In Swift I was able to do this by writing:

在 Swift 中,我可以通过写来做到这一点:

let tbc : UITabBarController = self.window?.rootViewController as UITabBarController
var moreViewController : UINavigationController = tbc.moreNavigationController

moreViewController.navigationBar.barStyle = UIBarStyle.Black

Basically you're interested in the last line.
This resulted in tabbar changing to white:

基本上你对最后一行感兴趣。
这导致标签栏变为白色:

enter image description here

在此处输入图片说明

Note that I didn't change anything in Info.plistin order to achieve this result.
For more informations regarding changing Navigation Status Bar, please check out this link: http://www.appcoda.com/customize-navigation-status-bar-ios-7/

请注意,Info.plist为了实现此结果,我没有进行任何更改。
有关更改的更多信息Navigation Status Bar,请查看此链接:http: //www.appcoda.com/customize-navigation-status-bar-ios-7/

回答by Jota Freitas Jr

On viewDidLoad method, put this:

在 viewDidLoad 方法上,把这个:

Objective C

目标 C

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
[self setNeedsStatusBarAppearanceUpdate];

Swift

迅速

UIApplication.shared.statusBarStyle = .lightContent
self.setNeedsStatusBarAppearanceUpdate()

回答by SoftDesigner

I bet you have your view controller embedded into a navigation controller. To avoid setting the navigation bar's style to .Blackuse this subclass:

我敢打赌,您已将视图控制器嵌入到导航控制器中。为避免将导航栏的样式设置为.Black使用此子类:

class YourNavigationController: UINavigationController {
    override func childViewControllerForStatusBarStyle() -> UIViewController? {
        return topViewController
    }
}

回答by Darkseal

Swift:

迅速:

let tbc : UITabBarController = self.window?.rootViewController as UITabBarController
var moreViewController : UINavigationController = tbc.moreNavigationController
moreViewController.navigationBar.barStyle = UIBarStyle.Black

Objective C:

目标 C:

append this to the controller.mfile viewDidLoad method:

将此附加到controller.m文件 viewDidLoad 方法:

[self setNeedsStatusBarAppearanceUpdate].

then implement this method in that same controller.mfile:

然后在同一个controller.m文件中实现这个方法:

- (UIStatusBarStyle) preferredStatusBarStyle { 
    return UIStatusBarStyleLightContent; 
}

Official docs:

官方文档:

https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TransitionGuide/Bars.html

https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TransitionGuide/Bars.html

Article I wrote on my blog:

我在博客上写的文章:

http://www.ryadel.com/2015/03/04/xcode-set-status-bar-style-and-color-in-objective-c/

http://www.ryadel.com/2015/03/04/xcode-set-status-bar-style-and-color-in-objective-c/

回答by jxdwinter

In the ViewController that you want to change the status bar's color

在要更改状态栏颜色的 ViewController 中

- (void) viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
}

- (void) viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}

回答by n13

Swift extension for this because I always forget how this works

Swift 扩展,因为我总是忘记它是如何工作的

extension UIViewController {
    // utility to set the status bar appearance
    // Note: Make sure "View controller-based status bar appearance" is set to NO in your target settings or this won't work
    func setStatusBarForDarkBackground(dark: Bool) {
        UIApplication.sharedApplication().statusBarStyle = dark ? .LightContent : .Default
        setNeedsStatusBarAppearanceUpdate()
    }
}

回答by dengApro

Xcode 10.3,

Xcode 10.3,

In iPhone 8 12.4 Simulator, it is OK.

在 iPhone 8 12.4 模拟器中,没问题。

In iPhone X 12.4 Simulator, I tried all above, not OK.

在 iPhone X 12.4 Simulator 中,我尝试了以上所有方法,还是不行。

Then I add it by hand, status bar consists of time, battery, cellular.

然后我手动添加,状态栏由时间、电池、蜂窝组成。

11

11

StatusBarView

状态栏视图


class StatusBottomView: UIView {
    private var batteryView:BatteryView!
    private var timeLabel:UILabel!
    private var timer:Timer?

    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubviews()
    }

    private func addSubviews() {
        batteryView = BatteryView()
        batteryView.tintColor = UIColor.blue
        addSubview(batteryView)
        timeLabel = UILabel()
        timeLabel.textAlignment = .center
        timeLabel.font = UIFont.systemFont(ofSize: 12)
        timeLabel.textColor = UIColor.blue
        addSubview(timeLabel)
        didChangeTime()
        addTimer()
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        let h = frame.size.height
        let x: CGFloat = 80
        batteryView.frame.origin = CGPoint(x: ScreenHeight - x - DZMBatterySize.width, y: (h - DZMBatterySize.height) / 2)
        timeLabel.frame = CGRect(x: x, y: 0, width: 50, height: h)
    }

    // MARK: -- Timer

    func addTimer() {
        if timer == nil {
            timer = Timer.scheduledTimer(timeInterval: 15, target: self, selector: #selector(didChangeTime), userInfo: nil, repeats: true)
            RunLoop.current.add(timer!, forMode: .common)
        }
    }


    func removeTimer() {
        if timer != nil {
            timer!.invalidate()
            timer = nil
        }
    }


    @objc func didChangeTime() {
        timeLabel.text = TimerString("HH:mm")
        batteryView.batteryLevel = UIDevice.current.batteryLevel
    }

}

BatteryLevelView

电池电量视图

class BatteryView: UIImageView {

    override var tintColor: UIColor! {

        didSet{ batteryLevelView.backgroundColor = tintColor }
    }

    /// BatteryLevel
    var batteryLevel:Float = 0 {

        didSet{ setNeedsLayout() }
    }

    /// BatteryLevelView
    private var batteryLevelView:UIView!

    convenience init() {

        self.init(frame: CGRect(x: 0, y: 0, width: DZMBatterySize.width, height: DZMBatterySize.height))
    }


    override init(frame: CGRect) {

        super.init(frame: CGRect(x: 0, y: 0, width: DZMBatterySize.width, height: DZMBatterySize.height))

        addSubviews()
    }

    func addSubviews() {

        batteryLevelView = UIView()
        batteryLevelView.layer.masksToBounds = true
        addSubview(batteryLevelView)

        image = UIImage(named: "battery_black")?.withRenderingMode(.alwaysTemplate)
        tintColor = UIColor.white
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        let spaceW:CGFloat = 1 * (frame.width / DZMBatterySize.width) * HJBatteryLevelViewScale
        let spaceH:CGFloat = 1 * (frame.height / DZMBatterySize.height) * HJBatteryLevelViewScale

        let batteryLevelViewY:CGFloat = 2.1*spaceH
        let batteryLevelViewX:CGFloat = 1.4*spaceW
        let batteryLevelViewH:CGFloat = frame.height - 3.4*spaceH
        let batteryLevelViewW:CGFloat = frame.width * HJBatteryLevelViewScale
        let batteryLevelViewWScale:CGFloat = batteryLevelViewW / 100

        var tempBatteryLevel = batteryLevel

        if batteryLevel < 0 {

            tempBatteryLevel = 0

        }else if batteryLevel > 1 {

            tempBatteryLevel = 1

        }

        batteryLevelView.frame = CGRect(x: batteryLevelViewX , y: batteryLevelViewY, width: CGFloat(tempBatteryLevel * 100) * batteryLevelViewWScale, height: batteryLevelViewH)
        batteryLevelView.layer.cornerRadius = batteryLevelViewH * 0.125
    }

}