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

时间:2013-09-25 19:50:48

标签: ios cocoa-touch


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

有人有提示吗?

我尝试了UIViewControllerBasedStatusBarAppearance

-(UIStatusBarStyle)preferredStatusBarStyle{ 
    return UIStatusBarStyleLightContent; 
}

但这些方法不起作用。

10 个答案:

答案 0 :(得分:138)

你试过这个吗?

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

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

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

    - (UIStatusBarStyle) preferredStatusBarStyle { 
        return UIStatusBarStyleLightContent; 
    }
    

答案 1 :(得分:72)

如果您的视图控制器位于独立的UINavigationController内部并且不是基于故事板的UINavigationController的一部分,那么这里有一个问题,那么所有方法都失败了。我遇到了这种情况,然后为了将状态栏设置为我在

后面使用的灯光样式
[self.navigationController.navigationBar setBarStyle:UIBarStyleBlack];

这对我很有用。

答案 2 :(得分:22)

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

UIViewControllerBasedStatusBarAppearance设置为NO,我可以使用以下方式将样式设置为白色文字:

[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackTranslucent;

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

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

[UIApplication sharedApplication].statusBarStyle = UIBarStyleBlack;

答案 3 :(得分:13)

在viewDidLoad方法上,输入:

目标C.

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

夫特

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

答案 4 :(得分:12)

在Swift中,我可以写下:

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

moreViewController.navigationBar.barStyle = UIBarStyle.Black

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

enter image description here

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

答案 5 :(得分:7)

我打赌你将视图控制器嵌入到导航控制器中。为避免将导航栏的样式设置为function(chart) { $('.btn').click(function() { chart.series[1].data[0].update({ color: 'red' }) }) } ,请使用此子类:

.Black

答案 6 :(得分:3)

<强>夫特:

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/

答案 7 :(得分:1)

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

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

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

答案 8 :(得分: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()
    }
}

答案 9 :(得分:0)

Xcode 10.3,

在iPhone 8 12.4 Simulator中,可以。

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

然后我手动添加它,状态栏包含时间,电池,手机。

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
    }

}