UINavigationBar

UINavigationBar 繼承于UIView 最好避免直接在上面添加界面元素,在push pop 中,系統不會處理,會一直存在。需要在viewWillAppear viewWillDisappear添加 刪除?

1. barStyle

UIBarStyleDefault(默認樣式)白色條,底部有一邊線,適用深色標題,狀態欄字體也是黑色,系統大多數應用都是這種樣式。

UIBarStyleBlack? 黑色條,底部有一邊線,適用于淺色的標題,狀態欄的字體也會是白色,iTunes Store 是這個樣式

還有兩種已經廢棄了的樣式?

UIBarStyleBlackOpaque ? ? ? Deprecated. Use UIBarStyleBlack ?

UIBarStyleBlackTranslucent ?Deprecated. Use UIBarStyleBlack and set the translucent property to YES

底部一像素的線


2.translucent ?(默認是YES ?iOS7出現的 設置導航條是否透明)

有設置自定義背景圖時可能出現的情況

1)默認依據圖來設置是否透明,當圖上只要有任意像素是alpha小于1,就YES

2)設置的背景是不透明的圖片,也能通過設置translucent 為YES,系統會使用一張opacity值小于1的圖

3)設置的背景是透明的圖片,設置translucent為NO,系統會使用一張不透明的背景圖


3.顏色,背景圖 (一般是設置了背景圖,設置的顏色就會被遮掉,透明的圖片會露出底色,注意層級關系)

self.navigationController.navigationBar.backgroundColor = [UIColor redColor]; ? ?//UINavigationBar

self.navigationController.navigationBar.barTintColor = [UIColor cyanColor];? ? ? //_UIBarBackground ? ? ? ? ? ? ? ? ?已廢棄的tintColor


[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"custom_navBar_opaque"] forBarMetrics:UIBarMetricsDefault]; //UIImageView

self.navigationController.navigationBar.translucent = NO;


self.navigationController.navigationBar.translucent = NO;



當沒有設置背景圖片的時候?


有設置背景圖片


4.shadowImage?

(默認是nil , 會顯示默認的一張shadow image)

當想用自定義的shadowImage替換默認圖片時,也要設置自定義背景圖,否者依然是默認陰影圖片


5.UINavigationBar默認有一個UINavigationItem

@property(nullable, nonatomic,readonly,strong) UINavigationItem *topItem;?

@property(nullable, nonatomic,readonly,strong) UINavigationItem *backItem; //默認是空


通過類別方法 self.navigationItem 拿到的也是默認的這個


6.UINavigationItem : NSObject ?

title?

titleView

prompt

backBarButtonItem

leftBarButtonItems

rightBarButtonItems

6.1 ? UIBarButtonItem : UIBarItem (導航欄上的按鈕,類似UIButton)

@property(nonatomic)? ? ? ? UIBarButtonItemStyle style;? ? ? ? ? ? // default is UIBarButtonItemStylePlain

@property(nullable, nonatomic,strong)? __kindof UIView? ? *customView;? ? ? // default is nil

@property(nullable, nonatomic)? ? ? ? SEL? ? ? ? ? ? ? ? ? action;? ? ? ? ? // default is NULL

@property(nullable, nonatomic,weak)? ? id? ? ? ? ? ? ? ? ? target;? ? ? ? ? // default is nil

6.2 UIBarItem : NSObject(類似UIView)

enabled

title

image

tag

imageInsets

landscapeImagePhoneInsets

- (void)setTitleTextAttributes:(nullable NSDictionary*)attributes forState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;




7.UIAppearance 協議

對實現了這個協議的類(UIView,UIBarItem)自定義這個類的所有實例的外觀,很牛逼的感覺

+ (instancetype)appearance; ?

也可以修改某一特定容器類中所有UIButton的外觀(如UIBarButtonItem)。

+ (instancetype)appearanceWhenContainedInInstancesOfClasses:(NSArray> *)containerTypes NS_AVAILABLE_IOS(9_0);


實現所有的navigationBar 都是同一個背景圖


需要注意的是,這種修改只會影響到那些執行UIAppearance操作之后添加到我們的視圖層級架構中的視圖或控件,而不會影響到修改之前就已經添加的對象



遺留:UINavigationBarDelegate

參考:http://www.cocoachina.com/ios/20150723/12671.html

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

推薦閱讀更多精彩內容