UINaviigationBar的使用

navigationBarHidden和navigationBar.hidden的區別

? ? ? ?navigationBarHidden在官方文檔上的解釋是A Boolean value that indicates whether the navigation bar is hidden

navigationBar在官方文檔上的解釋是It is permissible to customize the appearance of the navigation bar using the methods and properties of the UINavigationBar class but you must never change its frame, bounds, or alpha values or modify its view hierarchy directly. To show or hide the navigation bar, you should always do so through the navigation controller by changing its navigationBarHidden property or calling the setNavigationBarHidden:animated: method.

意思就是說它可以通過UINavigationBar這個類的屬性來自定義的顯示navigation bar,但是你不能改變他的坐標,大小,透明度或者改變它的等級.為了展示或者隱藏navigation bar,你應該通過改變navigationBarHidden屬性或者調取setNavigationBarHidden:animated:這個方法.

設置導航欄的背景顏色

在iOS 7中,不再使用tintColor屬性來設置導航欄的顏色,而是使用barTintColor屬性來修改背景色。我們可以在AppDelegate.m文件中的方法didFinishLaunchingWithOptions:里面添加如下代碼來修改顏色:

[[UINavigationBar appearance] setBarTintColor:[UIColor yellowColor]];

默認情況下,導航欄的translucent屬性為YES,但是在一般的開發過程中設置為NO

[[UINavigationBar appearance] setTranslucent:NO];

在導航欄中使用背景圖片

如果希望在導航欄中使用一個圖片當做背景,那么你需要提供一個稍微高一點的圖片(這樣可以延伸到導航欄背后)。導航欄的高度從44 points(88 pixels)變為了64 points(128 pixels)。我們依然可以使用setBackgroundImage:方法為導航欄設置自定義圖片。如下代碼所示:


[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"nav_bg.png"] forBarMetrics:UIBarMetricsDefault];

定制返回按鈕的顏色

要想給返回按鈕著色,可以使用tintColor屬性

[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];

對比照


如果想要用自己的圖片替換V型,可以設置圖片的backIndicatorImage和backIndicatorTransitionMaskImage。如下代碼所示:

[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:@"back_btn.png"]];

[[UINavigationBar?appearance]?setBackIndicatorTransitionMaskImage:[UIImage?imageNamed:@"back_btn.png"]];

修改導航欄標題的字體

跟iOS 6一樣,我們可以使用導航欄的titleTextAttributes屬性來定制導航欄的文字風格。在text attributes字典中使用如下一些key,可以指定字體、文字顏色、文字陰影色以及文字陰影偏移量:

UITextAttributeFont – 字體key

UITextAttributeTextColor – 文字顏色key

UITextAttributeTextShadowColor – 文字陰影色key

UITextAttributeTextShadowOffset – 文字陰影偏移量key


標題效果圖

NSShadow *shadow = [[NSShadow alloc] init];

shadow.shadowColor?=?[UIColor?colorWithRed:0.0?green:0.0?blue:0.0?alpha:0.8];

shadow.shadowOffset?=?CGSizeMake(0,?1);

[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:

[UIColor?colorWithRed:245.0/255.0?green:245.0/255.0?blue:245.0/255.0?alpha:1.0],?NSForegroundColorAttributeName,

shadow,?NSShadowAttributeName,

[UIFont?fontWithName:@"HelveticaNeue-CondensedBlack"size:21.0],?NSFontAttributeName,?nil]];

修改電池電量條的風格

第一種方法:在iOS 7中,我們可以在每個view controller中overridingpreferredStatusBarStyle

-(UIStatusBarStyle)preferredStatusBarStyle

{

returnUIStatusBarStyleLightContent;

}

第二種:在UIApplication的statusBarStyle方法來設置狀態欄,不過,首先需要停止使用View controller-based status bar appearance。在project target的Info tab中,插入一個新的key,名字為View controller-based status bar appearance,并將其值設置為NO

info

然后就可以使用下面的代碼來設置狀態欄風格

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

隱藏狀態欄

- (BOOL)prefersStatusBarHidden

{

returnYES;

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 前言 由于最近兩個多月,筆者正和小伙伴們忙于對公司新項目的開發,筆者主要負責項目整體架構的搭建以及功能模塊的分工。...
    CoderMikeHe閱讀 27,151評論 74 270
  • 本文為大地瓜原創,歡迎知識共享,轉載請注明出處。雖然你不注明出處我也沒什么精力和你計較。作者微信號:christg...
    大地瓜123閱讀 743評論 0 0
  • 本文為大地瓜原創,歡迎知識共享,轉載請注明出處。雖然你不注明出處我也沒什么精力和你計較。作者微信號:christg...
    大地瓜123閱讀 910評論 1 2
  • 概述 信用卡APP主要滿足的是持卡用戶還款、分期、搶優惠等需求。 此文分析的是有親爹背景的信用卡APP,案例產品是...
    阿爾伯特陳閱讀 3,264評論 0 13
  • 一直以來心里不安于現狀,但又渾渾噩噩,想干什么該干什么都沒琢磨清楚,以至于該好好學習的時候沒努力,該戀愛的年紀...
    夜頁閱讀 704評論 0 0