79


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

有人有提示吗?

我试过了UIViewControllerBasedStatusBarAppearance

-(UIStatusBarStyle)preferredStatusBarStyle{ 
    return UIStatusBarStyleLightContent; 
}

但这些方法不起作用。

4

10 回答 10

139

你试过这个吗?

  1. 在 Info.plist 中将“查看基于控制器的状态栏外观”( UIViewControllerBasedStatusBarAppearance) 设置为。YESYES是默认值,因此您也可以将此值保留在 plist 之外。)

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

  3. 实现preferredStatusBarStyle,返回你想要的这个视图控制器的状态栏样式。

    - (UIStatusBarStyle) preferredStatusBarStyle { 
        return UIStatusBarStyleLightContent; 
    }
    
于 2013-09-25T20:35:34.323 回答
76

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

[self.navigationController.navigationBar setBarStyle:UIBarStyleBlack];

这对我来说非常有效。

于 2014-02-02T12:21:05.137 回答
22

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

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

[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackTranslucent;

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

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

[UIApplication sharedApplication].statusBarStyle = UIBarStyleBlack;
于 2013-11-12T19:59:04.530 回答
14

在 Swift 中,我可以通过以下方式做到这一点:

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

moreViewController.navigationBar.barStyle = UIBarStyle.Black

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

在此处输入图像描述

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

于 2015-03-01T12:27:42.690 回答
13

在 viewDidLoad 方法上,把这个:

目标 C

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

迅速

UIApplication.shared.statusBarStyle = .lightContent
self.setNeedsStatusBarAppearanceUpdate()
于 2014-03-02T12:36:30.033 回答
7

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

class YourNavigationController: UINavigationController {
    override func childViewControllerForStatusBarStyle() -> UIViewController? {
        return topViewController
    }
}
于 2016-10-06T18:19:56.410 回答
5

迅速:

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

目标 C:

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

[self setNeedsStatusBarAppearanceUpdate].

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

- (UIStatusBarStyle) preferredStatusBarStyle { 
    return UIStatusBarStyleLightContent; 
}

官方文档:

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

我在博客上写的文章:

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

于 2015-03-04T02:31:40.567 回答
1

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

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

- (void) viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}
于 2015-09-25T06:24:28.200 回答
0

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()
    }
}
于 2016-09-15T18:31:23.650 回答
0

Xcode 10.3,

在 iPhone 8 12.4 Simulator 中是可以的。

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

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

11

状态栏视图


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
    }

}

电池电量查看

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
    }

}


于 2019-07-27T05:01:54.797 回答