这里是Apple Guidelines/Instruction关于状态栏更改。
如果您想设置状态栏样式,应用程序级别然后在您的.plist 文件中将UIViewControllerBasedStatusBarAppearance 设置为NO。并在您的 appdelegate > didFinishLaunchingWithOptions 添加以下代码(以编程方式,您可以从应用程序委托中完成)。
目标 C
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
斯威夫特
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
application.statusBarStyle = .lightContent
return true
}
如果您想设置状态栏样式,请在视图控制器级别执行以下步骤:
- 如果只需要在 UIViewController 级别设置状态栏样式,请在
.plist 文件中将 UIViewControllerBasedStatusBarAppearance 设置为 YES。
在viewDidLoad添加函数-setNeedsStatusBarAppearanceUpdate
在您的视图控制器中覆盖 preferredStatusBarStyle。
目标 C
- (void)viewDidLoad
{
[super viewDidLoad];
[self setNeedsStatusBarAppearanceUpdate];
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
斯威夫特
override func viewDidLoad() {
super.viewDidLoad()
self.setNeedsStatusBarAppearanceUpdate()
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
根据状态栏样式设置级别设置.plist的值。
您可以在应用程序启动期间或视图控制器的 viewDidLoad 期间设置状态栏的背景颜色。
extension UIApplication {
var statusBarView: UIView? {
return value(forKey: "statusBar") as? UIView
}
}
// Set upon application launch, if you've application based status bar
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
return true
}
}
or
// Set it from your view controller if you've view controller based statusbar
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
}
}
结果如下: