UINavigationBar

一、簡介

<<UINavigationBar類實現了一個層次的內容導航控制。它的一個條形欄,通常顯示在屏幕上方,包含用于導航的向上和向下一個層次的按鈕。主要屬性是左(后退)按鈕,中心的名稱和一個可選的右邊的按鈕。您可以指定這些自定義視圖

<<UINavigationBar類實現了一個層次的內容導航控制。它的一個條形欄,通常顯示在屏幕上方,包含用于導航的向上和向下一個層次的按鈕。主要屬性是左(后退)按鈕,中心的名稱和一個可選的右邊的按鈕。您可以指定這些自定義視圖。

<<繼承關系:UINavigationBar --> UIView?-->UIResponder-->NSObject

格式為

1--> 設置UINavigationBar的樣式(屬性的作用)

typedef NS_ENUM(NSInteger, UIBarStyle) {

? ? UIBarStyleDefault? ? ? ? ? = 0,

? ? UIBarStyleBlack? ? ? ? ? ? = 1,

? ? UIBarStyleBlackOpaque? ? ? = 1, // Deprecated. Use UIBarStyleBlack

? ? UIBarStyleBlackTranslucent = 2, // Deprecated. Use UIBarStyleBlack and set the translucent property to YES

} __TVOS_PROHIBITED;

;(如果屬性有枚舉類型的話,這里會有枚舉類型說明)

navigationBar.barStyle =UIBarStyleBlack;(這是具體的例子)

@property(nonatomic) UIBarStyle barStyle UI_APPEARANCE_SELECTOR __TVOS_PROHIBITED; (這是屬性的說明)

二、UINavigationBar的屬性(屬性的順序與蘋果API一致)

1--> 設置UINavigationBar的樣式

typedef NS_ENUM(NSInteger, UIBarStyle) {

? ? UIBarStyleDefault? ? ? ? ? = 0,//默認樣式

? ? UIBarStyleBlack? ? ? ? ? ? = 1,//黑色

? ? UIBarStyleBlackOpaque? ? ? = 1, // 棄用屬性

? ? UIBarStyleBlackTranslucent = 2, //?棄用屬性

} __TVOS_PROHIBITED;

toolBar.barStyle =UIBarStyleBlack;

@property(nonatomic) UIBarStyle barStyle UI_APPEARANCE_SELECTOR __TVOS_PROHIBITED;?

2-->聲明UINavigationBarDelegate代理

navigationBar .delegate = self;//聲明代理

@property(nullable, nonatomic, weak) id<UINavigationBarDelegate>?delegate; //設置UINavigationBarDelegate代理

3-->設置UINavigationBar是否透明

navigationBar.translucent =NO;

@property(nonatomic,assign,getter=isTranslucent) BOOL translucent NS_AVAILABLE_IOS(3_0) UI_APPEARANCE_SELECTOR; //iOS 6和之前,默認情況下為NO。如果barStyle將UIBarStyleBlackTranslucent總是YES。

4、將新的UINavigationItem 壓入棧

[navigationBar pushNavigationItem:item animated:NO];

- (void)pushNavigationItem:(UINavigationItem *)item animated:(BOOL)animated;

4、將頂部的UINavigationItem 推出棧

[navigationBar?pushNavigationItem:item animated:NO];

- (nullable UINavigationItem *)popNavigationItemAnimated:(BOOL)animated; //

返回已彈出的項。

5、當前push到最上層的item

??UINavigationItem?*theTop=[navigationBar?topItem];

@property(nullable, nonatomic,readonly,strong) UINavigationItem *topItem;//只讀屬性

6、僅次于最上層的item,一般式被推向導航欄左側的item

??UINavigationItem?*theTop=[navigationBar?backItem];

@property(nullable, nonatomic,readonly,strong) UINavigationItem *backItem;//只讀屬性

7、獲取堆棧中所有item的數組

? NSArray *array=[navigationBar?items];

@property(nullable,nonatomic,copy) NSArray<UINavigationItem *> *items;

8、設置一組item

????[navigationBar?setItems:items?animated:NO];??

- (void)setItems:(nullable NSArray *)items animated:(BOOL)animated;// 如果動畫是YES,則模擬一個push或pop,這取決于新的top項目是否在堆棧中。

9、是否顯示大標題

self.navigationController.navigationBar.prefersLargeTitles=YES;//這句話表示是否顯示大標題

@property (nonatomic, readwrite, assign) BOOL prefersLargeTitles;

10、設置渲染顏色,會影響選中字體和圖案的渲染

navigationBar.tintColor=[UIColor redColor];

@property(null_resettable, nonatomic,strong) UIColor *tintColor;

11、設置導航欄的顏色

navigationBar.barTintColor = [UIColor whiteColor];

@property(nullable, nonatomic, strong) UIColor *barTintColor NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;//?barTintColor會影響背景顏色, iOS7出現的新屬性,用來代替tintColor的作用

12、通過導航欄位置和量度設置背景圖片

//設置透明的背景圖,便于識別底部線條有沒有被隱藏

[navigationBar setBackgroundImage:[[UIImage alloc] init] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];

- (void)setBackgroundImage:(nullable UIImage *)backgroundImage forBarPosition:(UIBarPosition)barPosition barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;

13、通過導航欄位置和量度獲取背景圖片

UIImage *image=[navBar backgroundImageForBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];

- (nullable UIImage *)backgroundImageForBarPosition:(UIBarPosition)barPosition barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;

14、通過導航欄量度設置背景圖片

[navigationBar setBackgroundImage:[[UIImage alloc] init] for BarMetrics:UIBarMetricsDefault];

- (void)setBackgroundImage:(nullable UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

15、通過導航欄量度獲取背景圖片

?UIImage *image=[navBar backgroundImageForBarMetrics:UIBarMetricsDefault];

- (nullable UIImage *)backgroundImageForBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

16、設置導航欄的陰影圖片

[navigationBar?setShadowImage:[UIImage?new]];

@property(nullable, nonatomic,strong) UIImage *shadowImage NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR;

17、設置導航欄的標題字體屬性

navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor redColor]};

@property(nullable,nonatomic,copy) NSDictionary *titleTextAttributes NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

18、設置大標題的文本樣式

[self.navigationController.navigationBar?setLargeTitleTextAttributes:[NSDictionarydictionaryWithObjectsAndKeys:[UIColor?whiteColor], NSForegroundColorAttributeName,[UIFontsystemFontOfSize:18.0f],NSFontAttributeName,nil]];

@property(nullable, nonatomic, copy) NSDictionary *largeTitleTextAttributes UI_APPEARANCE_SELECTOR API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos);

19、設置導航欄標題的豎直位置偏移

[navBar setTitleVerticalPositionAdjustment:10 forBarMetrics:UIBarMetricsDefault];//adjustment指定了偏移量,正值為向下偏移,負值為向上偏移,如設置當前title向下偏移10個像素

- (void)setTitleVerticalPositionAdjustment:(CGFloat)adjustment forBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

20、獲取導航欄標題的豎直位置偏移

CGFloat adjust=[navBar titleVerticalPositionAdjustmentForBarMetrics:UIBarMetricsDefault];

- (CGFloat)titleVerticalPositionAdjustmentForBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

21、設置背景指示器圖片(返回按鈕的樣式,這兩個一起設置)

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

@property(nullable,nonatomic,strong) UIImage *backIndicatorImage NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR __TVOS_PROHIBITED;//導航欄左側pop按鈕的圖案默認是一個箭頭,我們可以使用這個的方法修改

22、設置背景指示器圖片(返回按鈕的樣式,這兩個一起設置)

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

@property(nullable,nonatomic,strong) UIImage *backIndicatorTransitionMaskImage NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR __TVOS_PROHIBITED;//?通常你說的這兩個方法設置的圖片應該是一樣的, 除非你想讓backbutton在點擊后的效果有所不同。

三、UINavigationBar的代理屬性

1-->item將要push的時候調用,返回NO,則不能push

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPushItem:(UINavigationItem *)item{

return NO;

}

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPushItem:(UINavigationItem *)item;?

2-->item已經push后調用

- (void)navigationBar:(UINavigationBar *)navigationBar didPushItem:(UINavigationItem *)item{

}

- (void)navigationBar:(UINavigationBar *)navigationBar didPushItem:(UINavigationItem *)item; // 在動畫結束時被調用

3->?item將要pop時調用,返回NO,不能pop

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item{

return NO;

}

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item;?

4->?item已經pop后調用

- (void)navigationBar:(UINavigationBar *)navigationBar didPopItem:(UINavigationItem *)item{

}

- (void)navigationBar:(UINavigationBar *)navigationBar didPopItem:(UINavigationItem *)item;

參考

UINavigationBar 一個有樣式的view

ios開發UI 控件——UINavigationBar 與UINavigationController

navigationBar

UInavigationBar上添加左箭頭的返回按鈕

iOS開發UINavigation——導航控制器UINavigationController

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,963評論 6 542
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,348評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,083評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,706評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,442評論 6 412
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,802評論 1 328
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,795評論 3 446
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,983評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,542評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,287評論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,486評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,030評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,710評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,116評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,412評論 1 294
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,224評論 3 398
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,462評論 2 378

推薦閱讀更多精彩內容