我尝试了多种方法来设置状态栏的样式(默认或浅色内容),但无法在每个视图控制器上单独实现。我只能为整个应用程序设置状态栏样式。
有人有提示吗?
我尝试过UIViewControllerBasedStatusBarAppearance
和
-(UIStatusBarStyle)preferredStatusBarStyle{
return UIStatusBarStyleLightContent;
}
但是这些方法不起作用。
我尝试了多种方法来设置状态栏的样式(默认或浅色内容),但无法在每个视图控制器上单独实现。我只能为整个应用程序设置状态栏样式。
有人有提示吗?
我尝试过UIViewControllerBasedStatusBarAppearance
和
-(UIStatusBarStyle)preferredStatusBarStyle{
return UIStatusBarStyleLightContent;
}
但是这些方法不起作用。
你试过这个吗?
在你的 Info.plist 中将“View controller-based status bar appearance”(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
就会接管一切。 - Desmond DAI编辑:此解决方案在iOS 9上已过时。请从其他答案中选择一个。
当UIViewControllerBasedStatusBarAppearance
设置为NO时,我可以使用以下方式将样式设置为白色文本:
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackTranslucent;
这是因为在iOS 6及以下版本中,此样式的文本颜色是白色。
更新:根据@jowie的说法,您可以在iOS8上尝试此操作:
[UIApplication sharedApplication].statusBarStyle = UIBarStyleBlack;
UIBarStyleBlack
。 - jowielet tbc : UITabBarController = self.window?.rootViewController as UITabBarController
var moreViewController : UINavigationController = tbc.moreNavigationController
moreViewController.navigationBar.barStyle = UIBarStyle.Black
基本上,您对最后一行感兴趣。
这导致标签栏变为白色:
请注意,我没有改变Info.plist
中的任何内容就实现了这个结果。
有关更改导航状态栏
的更多信息,请查看此链接:
http://www.appcoda.com/customize-navigation-status-bar-ios-7/
Objective C
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
[self setNeedsStatusBarAppearanceUpdate];
快捷
UIApplication.shared.statusBarStyle = .lightContent
self.setNeedsStatusBarAppearanceUpdate()
我打赌你将你的视图控制器嵌入了一个导航控制器。为了避免设置导航栏样式为.Black
,请使用这个子类:
class YourNavigationController: UINavigationController {
override func childViewControllerForStatusBarStyle() -> UIViewController? {
return topViewController
}
}
Swift:
let tbc : UITabBarController = self.window?.rootViewController as UITabBarController
var moreViewController : UINavigationController = tbc.moreNavigationController
moreViewController.navigationBar.barStyle = UIBarStyle.Black
Objective 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/
这里有两个链接,一个是苹果官方的文档,另一个是关于如何在Objective-C中设置状态栏风格和颜色的博客文章。您可以访问这些链接以获取更多信息。- (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 模拟器中,一切正常。
在 iPhone X 12.4 模拟器中,我尝试了所有方法,但都不行。
然后我手动添加了状态栏,包括时间、电池和蜂窝网络。
状态栏视图
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
}
}