iOS 關于狀態(tài)欄、導航欄的幾處筆記

級別: ★☆☆☆☆
標簽:「iOS」「狀態(tài)欄」「導航欄」
作者: dac_1033
審校: QiShare團隊


狀態(tài)欄與導航欄

狀態(tài)欄與導航欄的位置如上圖,我們可以通過[UIApplication sharedApplication].statusBarFrame.size獲取狀態(tài)欄的size(一般沒有劉海時的高度為20,有劉海時的高度為44)。
通過self.navigationController.navigationBar.frame.size獲取導航欄的size(一般高度為44,大標題時高度為xyz,當然也可以通過自定義來改變導航欄樣式)。
ps:在我們通過[nav.navigationBar setBarTintColor:[UIColor lightGrayColor]];來設置導航欄顏色時,將導致導航欄和狀態(tài)欄背景色均變?yōu)闇\灰色。

1. 狀態(tài)欄內(nèi)容是否高亮

狀態(tài)欄內(nèi)容包括信號、時間、電量等,只有兩種顏色樣式(黑或白)。iOS開發(fā)過程中提供修改狀態(tài)欄內(nèi)容顏色樣式的方法:

  • 在代碼中設置狀態(tài)欄內(nèi)容顏色:info.plist文件中直接設置狀態(tài)欄內(nèi)容顏色:在info.plist中添加字段View controller-based status bar appearance, 當其取值為YES時,表示以UIController對狀態(tài)欄的設置為準,UIApplication對狀態(tài)欄進行的設置將不起作用。
// 在controller中重寫該方法,并返回相應值
- (UIStatusBarStyle)preferredStatusBarStyle {
    
    return UIStatusBarStyleLightContent;
}

// 注意:
// 當controller嵌入UINavigationController中時,controller中的方法preferredStatusBarStyle是不會自動被調(diào)用的,而navController中的該方法會被調(diào)用;
// 當有navController時,在重寫controller中的preferredStatusBarStyle方法同時,我們還應重寫navController中的childViewControllerForStatusBarStyle方法。
- (UIViewController *)childViewControllerForStatusBarStyle {
    
    return self.topViewController;
}

當字段View controller-based status bar appearance取值為NO時,則以UIApplication為準,控制器設置狀態(tài)欄的方法preferredStatusBarStyle則根本不會被調(diào)用。

// 狀態(tài)欄內(nèi)容-黑色
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
// 狀態(tài)欄內(nèi)容-白色
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
  • 在info.plist文件中直接設置狀態(tài)欄內(nèi)容顏色:字段View controller-based status bar appearance取值為NO,且Status bar style取值為UIStatusBarStyleLightContent(可選),則不寫代碼,也可控制應用中的狀態(tài)欄內(nèi)容顏色。

2. 隱藏狀態(tài)欄

  • 整個項目隱藏
    在Targets -> General -> 勾選 Hide status bar 即可。


    工程配置
  • 在單個界面中隱藏

// iOS 9.0 之前
// 隱藏=YES,顯示=NO; Animation:動畫效果
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];

// iOS 9.0 之后推薦使用這個
// 注意:該方法可以通過UIController中的方法setNeedsStatusBarAppearanceUpdate來間接調(diào)用
- (BOOL)prefersStatusBarHidden {
    
    return YES;
}

注意:上面兩個操作狀態(tài)方法依然受到Info.plist中字段View controller-based status bar appearance取值得影響,該字段取值為YES時,進入controller會自動調(diào)用prefersStatusBarHidden方法。當controller嵌入UINavigationController中時,controller中的方法prefersStatusBarHidden是不會自動被調(diào)用的,而navController中的該方法會被調(diào)用;當有navController時,在重寫controller中的prefersStatusBarHidden方法同時,我們還應重寫navController中的childViewControllerForStatusBarHidden方法。

- (UIViewController *)childViewControllerForStatusBarHidden {
    
    return self.topViewController;
}
  • 啟動頁隱藏狀態(tài)欄,進入程序后正常顯示狀態(tài)欄
    首先,在Targets->General->勾選中Hide status bar或者在info.plist里面 Status bar is initially hidden 設置為 YES;
    其次,在AppDelegate.m中添加代碼
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [application setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
}

3. 導航欄

UINavigationController的視圖結構

UINavigationController的視圖層次結構比較清晰,用戶可見的導航欄即為其中的UINavigationBar,通過它,我們就可以修改導航欄的樣式。下面我們就逐步介紹一些常用的關于導航欄的操作。

  • 導航欄常用操作
// 隱藏導航欄
//[self.navigationController setNavigationBarHidden:YES];
[self.navigationController setNavigationBarHidden:YES animated:YES];

// 設置導航背景為紅色
[self.navigationController.navigationBar setBarTintColor:[UIColor redColor]];

// 設置navigationBar的透明效果
[self.navigationController.navigationBar setTranslucent:YES]; 
//設置導航欄的背景圖片
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"imgName"] forBarMetrics:UIBarMetricsDefault];

// Attributes 屬性
NSDictionary  *textAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:30]};
// 設置導航欄標題的字體大小、顏色
[self.navigationController.navigationBar setTitleTextAttributes:textAttributes];

4. 導航欄常用樣式

在日常開發(fā)中,我們經(jīng)常需要修改導航欄樣式,如使導航欄透明、導航欄尺寸等,在不同樣式導航欄之間的切換時還要注意是否平順...(隱藏,與正常導航欄的切換)

  • 導航欄大標題
if (@available(iOS 11.0, *)) {
        [self.navigationController.navigationBar setPrefersLargeTitles:YES];
}
  • 自定義導航欄大標題樣式
    重寫UINavigationBar中的layoutSubviews方法,可通過發(fā)送通知的形式來監(jiān)聽導航欄高度變化,如下:
-(void)layoutSubviews {
    [super layoutSubviews];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:KEY_UINavigationBar_Height_Changed object:self userInfo:nil];
}
  • 使導航欄透明
    當需要設置導航欄透明且title等項正常顯示時,最好將設置過程置于viewWillAppear:方法中:
//// 需要導航欄透明的ViewController中
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    _navView.hidden = NO;
    self.edgesForExtendedLayout = UIRectEdgeTop;
    self.navigationController.navigationBar.translucent = YES;
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}

//// 導航欄為非透明的ViewController中
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationController.navigationBar setShadowImage:nil];
    [self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor blackColor]}];
}

關于自定義導航欄、tabBar的例子將在后續(xù)文章中介紹...


推薦文章:
算法小專欄:遞歸與尾遞歸
iOS 避免常見崩潰(二)
算法小專欄:選擇排序
iOS Runloop(一)
iOS 常用調(diào)試方法:LLDB命令
iOS 常用調(diào)試方法:斷點
iOS 常用調(diào)試方法:靜態(tài)分析

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • 【iOS 開發(fā)】Status Bar 狀態(tài)欄設置匯總 狀態(tài)欄 個人覺得 iOS 的 Status Bar 狀態(tài)欄也...
    Cocojiang閱讀 970評論 0 1
  • IOS的項目多數(shù)會遇到控制狀態(tài)欄和導航欄的問題,比如隱藏狀態(tài)欄、控制狀態(tài)欄的文字顏色等,導航欄也有同樣需求。本文總...
    YHWXQ簡簡單單的生活閱讀 1,871評論 1 8
  • 一、有關狀態(tài)欄的隱藏 1.1、方式一:提示:控制器之間的狀態(tài)欄不會相互影響 (下面是 iOS9 之后的設置,不支持...
    IIronMan閱讀 10,876評論 0 15
  • 寫給希望永遠自信得體的女人! 何為優(yōu)雅? 優(yōu)雅是一種和諧,與美麗很相似。但美麗往往是一種自然的恩賜,而優(yōu)雅則是藝術...
    XUNQOOBEE閱讀 319評論 0 0
  • “美在丑的旁邊,畸形靠近優(yōu)美,丑怪藏在崇高背后,美與丑并存,光明與黑暗相關?!薄旯?拜讀完雨果大師的經(jīng)典名著《...
    宓美人閱讀 5,962評論 6 50

友情鏈接更多精彩內(nèi)容