一、簡介
<<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;
參考