我尝试了多种方法来设置状态栏样式(默认或lightcontent),但无法使其在每个视图控制器的基础上工作。我只能为整个应用设置状态栏样式。
有人暗示吗?
我试过了 UIViewControllerBasedStatusBarAppearance
和
-(UIStatusBarStyle)preferredStatusBarStyle{
return UIStatusBarStyleLightContent;
}
但是这些方法不起作用。
我尝试了多种方法来设置状态栏样式(默认或lightcontent),但无法使其在每个视图控制器的基础上工作。我只能为整个应用设置状态栏样式。
有人暗示吗?
我试过了 UIViewControllerBasedStatusBarAppearance
和
-(UIStatusBarStyle)preferredStatusBarStyle{
return UIStatusBarStyleLightContent;
}
但是这些方法不起作用。
Answers:
你有尝试过吗?
在Info.plist中,将“基于控制器的状态栏外观”(UIViewControllerBasedStatusBarAppearance
)设置为YES
。(这YES
是默认设置,因此您也可以将此值保留在plist之外。)
在您的viewDidLoad方法中,调用[self setNeedsStatusBarAppearanceUpdate]
。
实施preferredStatusBarStyle
,返回此视图控制器所需的状态栏样式。
- (UIStatusBarStyle) preferredStatusBarStyle {
return UIStatusBarStyleLightContent;
}
如果您的视图控制器位于独立的UINavigationController中,而不是基于Storyboard的UINavigationController的一部分,则这里存在一个问题,那么所有方法首先都会失败。我遇到了这种情况,然后为了将状态栏设置为我下面使用的浅色样式
[self.navigationController.navigationBar setBarStyle:UIBarStyleBlack];
这对我来说非常有效。
UINavigationBar.barStyle
覆盖preferredStatusBarStyle
。无论如何设置preferredStatusBarStyle
,一个该死的`UINavigationBar.setBarStyle'都会接管一切。
编辑:此解决方案在iOS 9上已弃用。请选择其他答案之一。
随着UIViewControllerBasedStatusBarAppearance
设置为NO,我能够通过设置样式为白色文字:
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackTranslucent;
这是因为此样式的文本颜色在iOS 6及更低版本上为白色。
更新:根据@jowie,您可以在iOS8上尝试:
[UIApplication sharedApplication].statusBarStyle = UIBarStyleBlack;
UIBarStyleBlack
。
在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/
在viewDidLoad方法上,输入以下内容:
目标C
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
[self setNeedsStatusBarAppearanceUpdate];
迅速
UIApplication.shared.statusBarStyle = .lightContent
self.setNeedsStatusBarAppearanceUpdate()
AppDelegate
)或每个视图控制器。
迅速:
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;
}
官方文档:
我在博客上写的文章:
http://www.ryadel.com/2015/03/04/xcode-set-status-bar-style-and-color-in-objective-c/
在ViewController中,您要更改状态栏的颜色
- (void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
}
- (void) viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}
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()
}
}
Xcode 10.3,
在iPhone 8 12.4 Simulator中,可以。
在iPhone X 12.4 Simulator中,我尝试了以上所有方法,但不行。
然后,我手动添加它,状态栏包括时间,电池,手机。
状态栏视图
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
}
}