[英]How to change the status bar background color and text color on iOS 7?
[英]How to change Status Bar text color in iOS
我的應用程序有一個黑暗的背景,但在 iOS 7 狀態欄變得透明。 所以我在那里什么也看不到,只有角落里的綠色電池指示燈。 如何像在主屏幕上一樣將狀態欄文本顏色更改為白色?
在 .plist 文件中將UIViewControllerBasedStatusBarAppearance
設置為YES
。
在viewDidLoad
做一個[self setNeedsStatusBarAppearanceUpdate];
添加以下方法:
- (UIStatusBarStyle)preferredStatusBarStyle { return UIStatusBarStyleLightContent; }
注意:這不適用於UINavigationController
內的控制器,請參閱下面泰森的評論:)
Swift 3 - 這將在UINavigationController
控制器。 將此代碼添加到您的控制器中。
// Preferred status bar style lightContent to use on dark background.
// Swift 3
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
Swift 5 和 SwiftUI
對於 SwiftUI,創建一個名為HostingController.swift
的新 swift 文件
import Foundation
import UIKit
import SwiftUI
class HostingController: UIHostingController<ContentView> {
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}
然后在SceneDelegate.swift
更改以下代碼行
window.rootViewController = UIHostingController(rootView: ContentView())
到
window.rootViewController = HostingController(rootView: ContentView())
或者,您可以選擇退出基於視圖控制器的狀態欄外觀:
Info.plist
中將View controller-based status bar appearance
為NO
。[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
注意:此方法在 iOS9 中已被棄用。 改為在 UIViewController 上使用preferredStatusBarStyle
。 (請參閱Apple 開發人員庫)
您無需編寫任何代碼行即可完成此操作!
執行以下操作使整個應用程序的狀態欄文本顏色為白色
在您的項目 plist文件中:
Transparent black style (alpha of 0.5)
NO
NO
注意:大多數贊成的答案不適用於 iOS 7 / 8
在 Info.plist 中將“基於控制器的狀態欄外觀”設置為 NO
在 AppDelegate 添加
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
到
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
......
...
}
此解決方案適用於 iOS 7 / 8
對我來說,使用其他答案(以及其他來源/文檔)中的所有內容都沒有發生任何事情。 有幫助的是在 XIB 中將導航欄樣式設置為“黑色”。 這將文本更改為白色,根本沒有任何代碼。
這些都不適合我,所以這里有一個可行的解決方案......
在Info.plist
,添加一行:
UIViewControllerBasedStatusBarAppearance
,並設置值NO
。
然后在didFinishLaunchingWithOptions
AppDelegate 中,添加以下行:
[application setStatusBarHidden:NO];
[application setStatusBarStyle:UIStatusBarStyleLightContent];
只需以下兩步:
第1步:
在project target
的信息選項卡下,添加行:
UIViewControllerBasedStatusBarAppearance
,設置值NO
。
第2步:
在AppDelegate.m
項目中:
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
…
[application setStatusBarStyle:UIStatusBarStyleLightContent];
…
}
這適用於 2013 年 9 月 18 日發布的 Golden Master iOS 7 和 Xcode 5 GM 種子和 iOS7 SDK(至少隱藏了導航控制器):
在Info.plist
the UIViewControllerBasedStatusBarAppearance
設置為NO
。
在ViewDidLoad
方法或任何地方,您想在哪里更改狀態欄樣式: [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
如果您的 UIViewController 在 UINavigationController 內,您將必須設置 BarStyle:
-[UINavigationBar setBarStyle:UIBarStyleBlack]
原答案在這里
如果您有通過Interface Builder創建的嵌入式導航控制器,請確保在管理導航控制器的類中設置以下內容:
-(UIStatusBarStyle)preferredStatusBarStyle{
return UIStatusBarStyleLightContent;
}
這應該就是你所需要的。
在 AppDelegate.m 中,添加以下內容。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}
在 Plist 文件中,將“基於控制器的狀態欄外觀”設置為“否”。
在 Swift 3 中非常簡單,只需 2 個步驟。 轉到您的 info.plist 並將關鍵的View controller-based status bar appearance
更改為“NO”。 然后在 Appdelegate 中只需在 didfinishlaunchingwithoptions 方法中添加這一行
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
UIApplication.shared.statusBarStyle = .lightContent
return true
}
這已在 iOS9 中被棄用,現在您應該在 rootviewcontroller 中覆蓋此屬性
這樣做在 iOS 9 中已被棄用應該在 rootviewcontroller 上這樣做
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
嗯,這對我來說真的是小菜一碟。
轉到您的應用程序的info.plist
。
View controller-based status bar appearance
為NO
Status bar style
UIStatusBarStyleLightContent
為UIStatusBarStyleLightContent
然后轉到您的應用程序的委托並粘貼以下代碼,您可以在其中設置 Windows 的 RootViewController。
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
{
UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0,320, 20)];
view.backgroundColor=[UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:1.0];
[self.window.rootViewController.view addSubview:view];
}
答對了。 它對我有用。
只需在 Appdelegate 中
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
iOS 7 允許單獨的視圖控制器確定狀態欄的外觀,如 Apple 開發人員文檔所述:
iOS 7 使視圖控制器能夠在應用程序運行時調整狀態欄的樣式。 動態更改狀態欄樣式的一個好方法是實現
preferredStatusBarStyle
並在動畫塊內更新狀態欄外觀並調用setNeedsStatusBarAppearanceUpdate
。
全局設置狀態欄外觀是一個兩步過程。
首先,您需要告訴 iOS 您不想在逐個視圖的基礎上設置狀態欄外觀。
然后你需要負責並實際設置新的全局狀態欄樣式。
要禁用逐視圖狀態欄控件,您需要在Info.plist
設置基於View controller-based status bar appearance
屬性。
打開 Project Navigator 並為您的 iOS 應用選擇項目,然后選擇 Info 選項卡。
將鼠標懸停在一行上,然后單擊出現的加號以將新屬性添加到.plist
。
在 Key 字段中輸入View controller-based status bar appearance
,然后確保 Type 字段設置為Boolean
。 最后,在值字段中輸入NO
。
要為狀態欄設置全局樣式,請在 Info 選項卡下添加另一個屬性,鍵為Status bar style
,Type 為String
,Value 為Opaque black style
。
這是一篇包含更多細節和一些示例代碼的博客文章:
http://codebleep.com/setting-the-status-bar-text-color-in-ios-7/
為 Xcode GM Seed 更新的答案:
在Info.plist
將View controller-based status bar appearance
為NO
在項目中,設置:
在 ViewDidLoad 中:
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
不需要做一些額外的事情,只需在您的 viewController 中編寫此代碼並獲得狀態欄顏色為白色
- (UIStatusBarStyle)preferredStatusBarStyle{return UIStatusBarStyleLightContent;}
我認為所有的答案都沒有真正指出問題,因為它們都適用於特定場景。 但是,如果您需要涵蓋所有情況,請遵循以下幾點:
根據您需要狀態欄燈樣式的位置,您應該始終牢記以下 3 點:
1)如果您需要在啟動屏幕或其他您無法控制的地方使用狀態欄(不是在視圖控制器中,而是在一些系統控制的元素/時刻,如啟動屏幕)您轉到您的項目設置
2) 如果導航控制器中有一個控制器,您可以在界面構建器中進行如下更改:
b) 然后將導航欄的樣式設置為“黑色”,因為這意味着您的狀態欄下方將有一個“黑色”-> 深色背景,因此會將狀態欄設置為白色
或者在代碼中執行如下
navigationController?.navigationBar.barStyle = UIBarStyle.Black
3) 如果你有單獨的控制器,它需要有自己的狀態欄樣式,並且它沒有作為 UINavigationController 嵌入到某些容器結構中
在控制器的代碼中設置狀態欄樣式:
這是有關狀態欄更改的Apple 指南/說明。 狀態欄中只允許深色和淺色(同時和黑色)。
這是 - 如何更改狀態欄樣式:
如果要設置狀態欄樣式、應用程序級別,請在“.plist”文件中將UIViewControllerBasedStatusBarAppearance
設置為NO
。
如果您想設置狀態欄樣式,請在視圖控制器級別執行以下步驟:
.plist
文件中將UIViewControllerBasedStatusBarAppearance
設置為YES
。 在viewDidLoad中添加函數setNeedsStatusBarAppearanceUpdate
覆蓋視圖控制器中的 preferredStatusBarStyle。
——
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
}
}
這是結果:
這在iOS 7 UI Transition Guide 中有記錄,您需要 Apple 開發人員 ID 才能直接訪問。 相關摘錄:
因為狀態欄是透明的,所以它后面的視圖是透明的。 [...] 使用
UIStatusBarStyle
常量來指定狀態欄內容應該是深色還是淺色:
UIStatusBarStyleDefault
顯示深色內容。 [...]
UIStatusBarStyleLightContent
顯示輕量內容。 當黑暗內容位於狀態欄后面時使用。
也可能感興趣:
在 iOS 7 中,您可以從單個視圖控制器控制狀態欄的樣式,並在應用程序運行時更改它。 要選擇此行為,請將
UIViewControllerBasedStatusBarAppearance
鍵添加到應用程序的Info.plist
文件中,並為其指定值YES
。
我絕對建議您查看該文檔,同樣,您可以使用您的 Apple 開發人員 ID 訪問該文檔。
簡單地打電話
[[UINavigationBar appearance] setBarStyle:UIBarStyleBlack];
在里面
-(BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
}
我的AppDelegate
方法在 iOS7 中非常適合我。
我做了一些不同的事情,它對我有用。
在代碼沒有變化的情況下,我確實像這樣配置了我的 .plist 文件:
我希望它有幫助。
對於每個視圖控制器,我將故事板中的“狀態欄”的模擬指標屬性從“推斷”更改為“輕量內容”
如果您仍然想在 info.plist 中使用View controller-based status bar appearance
設置為 YES,這意味着您可以更改每個視圖控制器的狀態欄,請在 ViewDidLoad 中的狀態欄中使用以下白色文本:
[[[self navigationController] navigationBar] setBarStyle:UIBarStyleBlackTranslucent];
在 info.plist 中設置字段值 NO View controller-based status bar appearance並在目標> 常規設置中設置狀態欄樣式燈。
如果你想用 Swift 得到同樣的結果,你可以在你的 AppDelegate.swift 文件中使用這個代碼:
UINavigationBar.appearance().barStyle = .BlackTranslucent
狀態欄的文本將是白色的 :-) !
對於 Swift 5,我添加了以下幾行:
override func viewDidAppear(_ animated: Bool) {
navigationController?.navigationBar.barStyle = .black
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
Xcode 似乎不斷改變這一點,所以這是最新的。
要將狀態欄更改為白色:
UIViewControllerBasedStatusBarAppearance
並將值設置為No
(false)。UIStatusBarStyle
並將值設置為UIStatusBarStyleLightContent
(即輕量內容)。我的應用程序具有深色背景,但是在iOS 7中狀態欄變為透明的。 所以我看不到任何東西,只有角落的綠色電池指示器。 如何將狀態欄文本的顏色更改為白色,就像在主屏幕上一樣?
總結一下,編輯您的項目Info.plist
並添加:
View controller-based status bar appearance
: NO
Status bar style
: Opaque black style
或者如果您有原始鍵/值 plist
UIViewControllerBasedStatusBarAppearance
: NO
UIStatusBarStyle
: Opaque black style
讓我給您一個完整的答案。 更改狀態欄文本顏色非常容易,但是在iOS 7中,特別是對於新手來說,這有點令人困惑。
如果您試圖通過選擇視圖控制器並轉到右側的“模擬度量”,將情節提要中的顏色從黑色更改為白色,它將無法正常工作,我也不知道為什么。 它應該通過這樣的更改來工作,但是無論如何都可以。
其次,您不會在plist中找到UIViewControllerBasedStatusBarAppearance屬性,但默認情況下不存在。 您必須通過單擊+按鈕將其自己添加,然后將其設置為NO。
iOS 7狀態欄文字顏色
最后,您必須轉到AppDelegate.m文件,並在didFinishLaunchingWithOptions方法中添加以下內容,並添加以下行:
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
這會將所有視圖控制器的顏色更改為白色。 希望這可以幫助!
完成這項工作的關鍵是只有全屏視圖控制器才能決定狀態欄的樣式。
如果您正在使用導航控制器並希望在每個視圖控制器的基礎上控制狀態欄,您將需要繼承 UINavigationController 並實現 preferredStatusBarStyle 以便它返回 topViewController 的首選項。
確保將故事板場景中的類引用從UINavigationController 更改為您的子類(例如下面示例中的MyNavigationController)。
(以下對我有用。如果您的應用程序是基於 TabBar 的,您將希望通過繼承 UITabBarController 來做類似的事情,但我還沒有嘗試過)。
@interface MyNavigationController : UINavigationController
@end
@implementation MyNavigationController
- (UIStatusBarStyle)preferredStatusBarStyle
{
return self.topViewController.preferredStatusBarStyle;
}
@end
對於Xcode 5.1:
在 .plist 中將“基於視圖控制器的狀態欄外觀”添加到NO 。
在 AppDelegate 中,添加:
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
就這樣!
斯威夫特 3 - Xcode 8。
如果您希望狀態欄最初隱藏在啟動屏幕上,請嘗試此操作,
第 1 步:將以下內容添加到info.plist
。
View controller-based status bar appearance
值NO
Status bar is initially hidden
值YES
第 2 步:在didFinishLaunchingWithOptions
方法中編寫它。
UIApplication.shared.isStatusBarHidden = false
UIApplication.shared.statusBarStyle = UIStatusBarStyle.lightContent
您可以從 info.plist 執行此操作:
1)“視圖控制器狀態欄外觀”設置為“否”
2)“狀態欄樣式”設置為“UIStatusBarStyleLightContent”
完畢
在 Info.plist 中將“基於控制器的狀態欄外觀”設置為 NO
在 AppDelegate 添加
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
更改所有 ViewController 的狀態欄文本顏色
迅捷 3
如果 Info.plist 中基於視圖控制器的狀態欄外觀 = YES
然后將此擴展用於所有 NavigationController
extension UINavigationController
{
override open var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}
如果沒有 UINavigationController 並且只有 UIViewController 則使用下面的代碼:
extension UIViewController
{
override open var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}
目標 c
創建類別類
對於 UIViewController
在 UIViewController+StatusBarStyle.h
@interface UIViewController (StatusBarStyle)
@end
在 UIViewController+StatusBarStyle.m 中
#import "UIViewController+StatusBarStyle.h"
@implementation UIViewController (StatusBarStyle)
-(UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
@end
對於 UINavigationController
在 UINavigationController+StatusBarStyle.h 中
@interface UINavigationController (StatusBarStyle)
@end
在 UINavigationController+StatusBarStyle.m 中
#import "UINavigationController+StatusBarStyle.h"
@implementation UINavigationController (StatusBarStyle)
-(UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
@end
在 iOS 8 中:添加NavigationController.NavigationBar.BarStyle = UIBarStyle.Black;
查看viewDidLoad
如果我使用 UINavigationController,我會在 iOS 9 和 Swift 2.0 中做到這一點
self.navigationController?.navigationBar.barStyle = UIBarStyle.Black
如果我使用模態轉場,我會做這個
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return .LightContent
}
如果您的應用程序需要默認具有UIStatusBarStyleLightContent
,但您仍然希望能夠在某些屏幕上使用UIStatusBarStyleDefault
,您可以選擇在控制器級別管理狀態欄顏色,但在這種情況下,您必須覆蓋preferredStatusBarStyle
在每個視圖控制器中(或在基本視圖控制器中實現它,所有其他視圖控制器都將從該控制器繼承)。 這是解決此問題的另一種方法:
UIViewControllerBasedStatusBarAppearance
設置為NO
UIStatusBarStyle
為UIStatusBarStyleLightContent
所有視圖控制器都將使用白色文本作為狀態欄。 現在僅在需要黑色文本狀態欄的視圖控制器中添加此方法:
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
}
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillAppear:animated];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}
從 Xcode 執行此操作的最簡單方法(無需任何編碼)是:
View controller-based status bar appearance
添加到您的 Info.plist 並將值設置為NO
。Deployment Info
找到Status Bar Style
的選項。 將此選項的值設置為Light
。 您將擁有White
狀態欄。
UINavigationController
是UIViewController
的子類(誰知道🙃)!
因此,當呈現嵌入在導航控制器中的視圖控制器時,您並沒有真正呈現嵌入的視圖控制器; 您正在展示導航控制器! UINavigationController
作為UIViewController
的子類,繼承了preferredStatusBarStyle
和childForStatusBarStyle
,您可以根據需要進行設置。
以下任何一種方法都應該有效:
info.plist
,添加以下屬性:
UIUserInterfaceStyle
(又名“用戶界面樣式”)覆蓋UINavigationController
preferredStatusBarStyle
preferredStatusBarStyle
( doc ) - 視圖控制器的首選狀態欄樣式子類化或擴展UINavigationController
class MyNavigationController: UINavigationController { override var preferredStatusBarStyle: UIStatusBarStyle { .lightContent } }
或者
extension UINavigationController { open override var preferredStatusBarStyle: UIStatusBarStyle { .lightContent } }
在UINavigationController
覆蓋childForStatusBarStyle
childForStatusBarStyle
( doc ) - 當系統需要使用視圖控制器來確定狀態欄樣式時調用“如果您的容器視圖控制器從其子視圖控制器之一派生其狀態欄樣式,[覆蓋此屬性] 並返回該子視圖控制器。如果您返回 nil 或不覆蓋此方法,則使用 self 的狀態欄樣式. 如果此方法的返回值發生更改,請調用 setNeedsStatusBarAppearanceUpdate() 方法。”
子類化或擴展UINavigationController
class MyNavigationController: UINavigationController { override var childForStatusBarStyle: UIViewController? { topViewController } }
或者
extension UINavigationController { open override var childForStatusBarStyle: UIViewController? { topViewController } }
你可以返回任何你喜歡的視圖控制器。 我推薦以下之一:
注意:如果您決定UINavigationController
,請記住通過 IB 中的身份檢查器將該類應用到您的導航控制器。
PS 這適用於 iOS 13 😎
這似乎是 Xcode 和 iOS 7 當前版本的問題。
蘋果開發者論壇上的一些相關內容是在蘋果開發者論壇的“iOS 7 Beta Livability”中搜索 UIStatusBarStyleLightContent *(目前有 32 個帖子)。
我在嘗試將其設置為輕量版本時遇到了它。
(這只是對 Aaron 回答的跟進。)
刪除 .plist 文件中基於視圖控制器的狀態欄外觀(如果您已創建)並重新創建它。
將狀態欄樣式設置為不透明黑色樣式
在 appDelegate 中的 didFinishLaunching 下添加以下代碼。
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
這對我有用:
在plist
中將UIViewControllerBasedStatusBarAppearance
設置為YES
rootViewController
需要方法實現
-(UIStatusBarStyle)preferredStatusBarStyle
因為我的rootViewController
是由 Cocoapods ( JASidePanelController
) 管理的,所以我通過一個類別添加了這個方法:
#import "JASidePanelController+StatusBarStyle.h"
@implementation JASidePanelController (StatusBarStyle)
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
@end
只需更改1) Info.plist
查看基於控制器的狀態欄appearance
-> NO
並寫入2)
[[UIApplication
sharedApplication]setStatusBarStyle:UIStatusBarStyleLightContent];
在
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
在 iOS 7 上,如果你想使用 UIViewControllerBasedStatusBarAppearance == YES,並且你的根視圖控制器是 UINavigationController,你應該繼承它並重載 childViewControllerForStatusBarStyle,例如,像這樣:
- (UIViewController*) childViewControllerForStatusBarStyle
{
return self.viewControllers.lastObject;
}
此后,將在推送的視圖控制器上調用 preferredStatusBarStyle。
您可以將其用於 iOS 6 和 7:
#ifdef __IPHONE_7_0
# define STATUS_STYLE UIStatusBarStyleLightContent
#else
# define STATUS_STYLE UIStatusBarStyleBlackTranslucent
#endif
[[UIApplication sharedApplication] setStatusBarStyle:STATUS_STYLE animated:YES];
我必須為 swift 和導航控制器做些什么
extension UINavigationController {
override open var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}
請試試這個
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
[application setStatusBarHidden:NO];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
statusBar.backgroundColor = [UIColor blackColor];
}
如果要將其設置為任何顏色,請使用以下代碼。
id statusBarWindow = [[UIApplication sharedApplication] valueForKey:@"statusBarWindow"];
id statusBar = [statusBarWindow valueForKey:@"statusBar"];
SEL setForegroundColor_sel = NSSelectorFromString(@"setForegroundColor:");
if([statusBar respondsToSelector:setForegroundColor_sel]) {
// iOS 7+
[statusBar performSelector:setForegroundColor_sel withObject:YourColorHere];
^^^^^^^^^^^^^
}
我知道我正在訪問私有 API,但是我已經在許多項目中使用了它並且 Apple 已經批准了它。
就在提交應用程序時,將此代碼發送到“評論”部分中的 Apple,並通知您正在使用此代碼更改狀態欄顏色。
是的,也不要忘記下面。
這是一個更好的解決方案擴展導航控制器並放入故事板
class NVC: UINavigationController {
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
override func viewDidLoad() {
super.viewDidLoad()
self.navigationBar.isHidden = true
self.navigationController?.navigationBar.isTranslucent = false
self.navigationBar.barTintColor = UIColor.white
setStatusBarColor(view : self.view)
}
func setStatusBarColor(view : UIView){
if #available(iOS 13.0, *) {
let app = UIApplication.shared
let statusBarHeight: CGFloat = app.statusBarFrame.size.height
let statusbarView = UIView()
statusbarView.backgroundColor = UIColor.black
view.addSubview(statusbarView)
statusbarView.translatesAutoresizingMaskIntoConstraints = false
statusbarView.heightAnchor
.constraint(equalToConstant: statusBarHeight).isActive = true
statusbarView.widthAnchor
.constraint(equalTo: view.widthAnchor, multiplier: 1.0).isActive = true
statusbarView.topAnchor
.constraint(equalTo: view.topAnchor).isActive = true
statusbarView.centerXAnchor
.constraint(equalTo: view.centerXAnchor).isActive = true
} else {
let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView
statusBar?.backgroundColor = UIColor.black
}
}
}
狀態欄顏色為黑色,文本為白色
此解決方案適用於使用新 SwiftUI Lifecycle / iOS 14.0 的應用程序:
我需要動態地改變狀態欄文字顏色和不能訪問window.rootViewController
因為SceneDelegate
不會為SwiftUI生命周期存在。
我終於找到了 Xavier Donnellon 這個簡單的解決方案: https : //github.com/xavierdonnellon/swiftui-statusbarstyle
將StatusBarController.swift
文件復制到您的項目中,並將您的主視圖包裝到RootView
:
@main
struct ProjectApp: App {
var body: some Scene {
WindowGroup {
//wrap main view in RootView
RootView {
//Put the view you want your app to present here
ContentView()
//add necessary environment objects here
}
}
}
}
然后你可以通過使用.statusBarStyle(.darkContent)
或.statusBarStyle(.lightContent)
視圖修飾符,或者直接調用UIApplication.setStatusBarStyle(.lightContent)
來更改狀態欄文本顏色。
不要忘記在 Info.plist 中將“基於控制器的狀態欄外觀”設置為“是”。
就我而言,沒有任何幫助。 我試圖在ViewController2
更改StatusBar
顏色,該顏色嵌入在NavigationController
,而后者又是從ViewController1
模態呈現的。 這種方式不起作用:
override var preferredStatusBarStyle: UIStatusBarStyle {
return .darkContent
}
什么也沒發生,直到我找到了這個解決方案:添加到ViewController1
這一行 -
navigationController.modalPresentationCapturesStatusBarAppearance = true
let navigationController = UINavigationController(rootViewController: viewController2)
navigationController.modalPresentationStyle = .overFullScreen
navigationController.modalTransitionStyle = .crossDissolve
navigationController.modalPresentationCapturesStatusBarAppearance = true
self.present(navigationController, animated: true)
因此,如果您有類似於ViewController1 presented ViewController2
導航方案,請嘗試ViewController1 presented ViewController2
modalPresentationCapturesStatusBarAppearance
屬性
此屬性的默認值為 false。
當您通過調用 present(_:animated:completion:) 方法呈現視圖控制器時,僅當呈現的控制器的 modalPresentationStyle 值為 UIModalPresentationStyle.fullScreen 時,狀態欄外觀控制才會從呈現轉移到呈現的視圖控制器。 通過將此屬性設置為 true,您可以指定呈現的視圖控制器控件狀態欄外觀,即使呈現為非全屏。
對於全屏顯示的視圖控制器,系統會忽略此屬性的值。
這個答案是在 hackingwithswift 網站的幫助下
適用於 iOS (13, *)
有時我們需要不同顏色的狀態欄,例如對於一個 ViewController 我們需要黑色狀態欄,而對於第二個 ViewController 我們需要白色狀態欄。 現在我們必須做什么? 我們需要在 ViewController 中添加這個和平的代碼
// MARK: - Variables
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
// MARK: - View Life Cycle
override func viewDidAppear(_ animated: Bool) {
setNeedsStatusBarAppearanceUpdate()
}
此代碼將更改該特定 ViewController 中狀態欄的淺色或白色。 我們可以在 preferredStatusBarStyle 中將其更改為 .dark
extension UIApplication {
var statusBarView: UIView? {
return value(forKey: "statusBar") as? UIView
}
}
非常簡單的方法來更改狀態欄顏色。 創建導航控制器的子類。
在視圖 didload 方法中編寫此代碼。 在所有視圖控制器中影響此代碼
self.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName :
[UIColor whiteColor],
NSFontAttributeName:[UIFont boldSystemFontOfSize:19]};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.