如何在 iOS 上用 Swift 更改状态栏(Status Bar)文本颜色?
大家好!我是 iOS 开发新手,使用 Swift 时遇到了更改状态栏(顶部显示时间、电池等的面板)文本颜色的问题。我需要文本为白色,因为我的应用背景是深色的,标准的黑色文本根本看不见。
我尝试了几种方法,但都没有成功。以下是我尝试过的:
1. 在 Info.plist 中添加了键 `UIViewControllerBasedStatusBarAppearance` 并设置为 `NO`。
2. 在 AppDelegate 的 `application(_:didFinishLaunchingWithOptions:)` 方法中写了:
```swift
UIApplication.shared.statusBarStyle = .lightContent
```
但这不起作用——Xcode 警告说 `statusBarStyle` 在 iOS 13 中已弃用。
3. 然后我尝试在我的 ViewController 中重写方法:
```swift
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
```
但即使这样也没用——状态栏仍然保持深色。也许我遗漏了什么?我使用的是 Xcode 15,iOS 17。