iOS開發UINavigation系列一——導航欄UINavigtionBar

iOS開發UINavigation系列一——導航欄UINavigtionBar

1、導航欄的使用

        在iOS開發中,咱們一般會使用導航控制器,導航控制器中封裝了一個UINavigationBar,實際上,咱們也能夠在不使用導航控制器的前提下,單獨使用導航欄,在UINavigationBar中,也有許多咱們能夠定製的屬性,用起來十分方便。數組

2、UINavigationBar的建立和風格類型

        導航欄繼承於UIView,因此咱們能夠像建立普通視圖那樣建立導航欄,好比咱們建立一個高度爲80的導航欄,將其放在ViewController的頭部,代碼以下:字體

UINavigationBar *bar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 80)];
[self.view addSubview:bar];

效果以下:動畫

        

咱們也能夠設置導航欄的風格屬性,從iOS6以後,UINavigationBar默認爲半透明的樣式,從上面也能夠看出,白色的導航欄下面透出些許背景的紅色。導航欄的風格屬性能夠經過下面的屬性來設置:atom

@property(nonatomic,assign) UIBarStyle barStyle;

UIBarStyle是一個枚舉,其中大部分的樣式都已棄用,有效果的只有以下兩個:spa

typedef NS_ENUM(NSInteger, UIBarStyle) {
    UIBarStyleDefault          = 0,//默認
    UIBarStyleBlack            = 1,//黑色
}

默認的風格就是咱們上面看到的白色的風格,黑色的風格效果瑞以下:代理

3、導航欄經常使用屬性和方法

        從上面咱們能夠看到,iOS6後導航欄默認都是半透明的,咱們能夠經過下面的bool值來設置這個屬性,設置爲NO,則導航欄不透明,默認爲YES:code

@property(nonatomic,assign,getter=isTranslucent) BOOL translucent;

下面一些方法用於設置NavigationBar及上面item的顏色相關屬性:對象

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

tintColor這個屬性會影響到導航欄上左側pop按鈕的圖案顏色和字體顏色,系統默認是以下顏色:繼承

@property(nullable, nonatomic,strong) UIColor *barTintColor;

BarTintColor用於設置導航欄的背景色,這個屬性被設置後,半透明的效果將失效:圖片

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

上面兩個方法用於設置和獲取導航欄的背景圖案,這裏須要注意,默認背景圖案是不作縮放處理的,因此咱們使用的圖片尺寸要和導航欄尺寸匹配,這裏面還有一個UIBarMetrics參數,這個參數設置設備的狀態,以下:

typedef NS_ENUM(NSInteger, UIBarMetrics) {
    UIBarMetricsDefault,//正常豎屏狀態
    UIBarMetricsCompact,//橫屏狀態
};
//設置導航欄的陰影圖片
@property(nullable, nonatomic,strong) UIImage *shadowImage;
//設置導航欄的標題字體屬性
@property(nullable,nonatomic,copy) NSDictionary<NSString *,id> *titleTextAttributes;

標題字體屬性會影響到導航欄的中間標題,以下:

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

咱們也能夠經過下面的屬性設置導航欄標題的豎直位置偏移:

- (void)setTitleVerticalPositionAdjustment:(CGFloat)adjustment forBarMetrics:(UIBarMetrics)barMetrics;
- (CGFloat)titleVerticalPositionAdjustmentForBarMetrics:(UIBarMetrics)barMetrics;

還有一個細節,導航欄左側pop按鈕的圖案默認是一個箭頭,咱們可使用下面的方法修改:

@property(nullable,nonatomic,strong) UIImage *backIndicatorImage;
@property(nullable,nonatomic,strong) UIImage *backIndicatorTransitionMaskImage;

4、導航欄中item的push與pop操做

        UINavigationBar上面不僅是簡單的顯示標題,它也將標題進行了堆棧的管理,每個標題抽象爲的對象在iOS系統中是UINavigationItem對象,咱們能夠經過push與pop操做管理item組。

//向棧中添加一個item,上一個item會被推向導航欄的左側,變爲pop按鈕,會有一個動畫效果
- (void)pushNavigationItem:(UINavigationItem *)item animated:(BOOL)animated;
//pop一個item
- (nullable UINavigationItem *)popNavigationItemAnimated:(BOOL)animated; 
//當前push到最上層的item
@property(nullable, nonatomic,readonly,strong) UINavigationItem *topItem;
//僅次於最上層的item,通常式被推向導航欄左側的item
@property(nullable, nonatomic,readonly,strong) UINavigationItem *backItem;
//獲取堆棧中全部item的數組
@property(nullable,nonatomic,copy) NSArray<UINavigationItem *> *items;
//設置一組item
- (void)setItems:(nullable NSArray<UINavigationItem *> *)items animated:(BOOL)animated;

5、UINavigationBarDelegate

        在UINavigationBar中,還有以下一個屬性:

@property(nullable,nonatomic,weak) id<UINavigationBarDelegate> delegate;

經過代理,咱們能夠監控導航欄的一些push與pop操做:

//item將要push的時候調用,返回NO,則不能push
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPushItem:(UINavigationItem *)item; 
//item已經push後調用
- (void)navigationBar:(UINavigationBar *)navigationBar didPushItem:(UINavigationItem *)item; 
//item將要pop時調用,返回NO,不能pop  
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item; 
//item已經pop後調用 
- (void)navigationBar:(UINavigationBar *)navigationBar didPopItem:(UINavigationItem *)item;

專一技術,熱愛生活,交流技術,也作朋友。

——琿少 QQ羣:203317592

相關文章
相關標籤/搜索