iOS UINavigationBar

一、導(dǎo)航欄外觀

外觀 屬性
bar的樣式 barStyle
bar的透明度 translucent
bar的顏色 barTintColor
bar上控件的顏色 tintColor
bar的背景圖片 backgroundImage

二、導(dǎo)航欄內(nèi)容

內(nèi)容 屬性
標(biāo)題 title
標(biāo)題視圖 titleView
左側(cè)按鈕 leftBarButtonItem
右側(cè)按鈕 rightBarButtonItem
返回按鈕 backBarButtonItem
返回按鈕指示圖像 backIndicatorImage
返回按鈕遮罩圖像 backIndicatorTransitionMaskImage

第三、效果顯示

  • 給viewController添加一個(gè)導(dǎo)航欄彤敛,默認(rèn)效果如下:


    默認(rèn)效果.png
  • 默認(rèn)添加一個(gè)子視圖
-(void)test1{
    //默認(rèn)樣式
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    //默認(rèn)透明度為透明[此時(shí)添加的控件frame從屏幕最上方開始算起]
    self.navigationController.navigationBar.translucent = YES;
    
    UIView * showView = [[UIView alloc]initWithFrame:CGRectMake(100, 0, 200, 200)];
    showView.backgroundColor = [UIColor purpleColor];
    [self.view addSubview:showView];
}
默認(rèn)添加效果.png

  • 透明度為不透明
//設(shè)置導(dǎo)航欄的barStyle和translucent
-(void)test2{
    //默認(rèn)樣式
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    //透明度為不透明[此時(shí)添加的控件frame從導(dǎo)航欄下面開始算起]
    self.navigationController.navigationBar.translucent = NO;
    
    UIView * showView = [[UIView alloc]initWithFrame:CGRectMake(100, 0, 200, 200)];
    showView.backgroundColor = [UIColor purpleColor];
    [self.view addSubview:showView];
   
}
不透明.png
  • edgesForExtendedLayout
self.edgesForExtendedLayout = UIRectEdgeNone;
透明2.png

第四了赌、導(dǎo)航欄的顏色設(shè)置:barTintColor

self.navigationController.navigationBar.barTintColor = [UIColor purpleColor];
bar顏色.png

第五、導(dǎo)航欄的控件顏色設(shè)置[默認(rèn)為藍(lán)色]tintColor

self.navigationController.navigationBar.tintColor = [UIColor redColor];

第六玄糟、設(shè)置背景圖片背景圖片不同的size會(huì)展示不同的效果

backgroundImage 
64像素時(shí)勿她,會(huì)包含狀態(tài)欄
44像素時(shí),只包含導(dǎo)航欄 
小于44時(shí)阵翎,會(huì)疊滿狀態(tài)欄和導(dǎo)航欄
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"圖片名"] forBarMetrics:UIBarMetricsDefault];

第七逢并、UIBarButtonItem

  • 初始化的變化
1之剧、initWithBarButtonSystemItem: target:  action: 
系統(tǒng)提供樣式改變  

UIBarButtonItem * right = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFastForward target:self action:@selector(pushNextController)];
self.navigationItem.rightBarButtonItem = right;

-(void)pushNextController{
    SecondViewController * secondVc = [[SecondViewController alloc]init];
    [self.navigationController pushViewController:secondVc animated:YES];
}
初始化一.png
2、initWithTitle: style: target: action: 
style設(shè)置為UIBarButtonItemStylePlain 

//使用title初始化砍聊,style不能使用邊框類型   
UIBarButtonItem * right = [[UIBarButtonItem alloc]initWithTitle:@"Right" style:UIBarButtonItemStylePlain target:self action:@selector(pushNextController)];
self.navigationItem.rightBarButtonItem = right;
初始化二.png
3背稼、initWithImage: style: target: action: 
圖像需要進(jìn)行渲染,默認(rèn)渲染成模版  

//默認(rèn)---渲染為模版時(shí)
UIBarButtonItem * right = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@""] style:UIBarButtonItemStylePlain target:self action:@selector(pushNextController)];

//渲染為原圖
UIBarButtonItem * right = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@""] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(pushNextController)];

第八玻蝌、設(shè)置title和titleView

//設(shè)置title
self.navigationItem.title = @"title";
設(shè)置titleView
self.navigationItem.titleView = [UIButton buttonWithType:UIButtonTypeContactAdd];
titleView.png
//導(dǎo)航欄的控件顏色設(shè)置[默認(rèn)為藍(lán)色]
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

self.navigationItem.titleView = [UIButton buttonWithType:UIButtonTypeContactAdd];
titleView2.png

第九蟹肘、導(dǎo)航欄設(shè)置backBarButtonItem

  • 沒有title,默認(rèn)顯示back
  • 設(shè)置title俯树,顯示title的內(nèi)容

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"回去" style:UIBarButtonItemStylePlain target:nil action:nil];

back.png

補(bǔ)充

導(dǎo)航欄中設(shè)置控件的image對(duì)象都需要進(jìn)行渲染設(shè)置
默認(rèn)是渲染為模版帘腹,需要渲染為原圖才能正常顯示。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末许饿,一起剝皮案震驚了整個(gè)濱河市阳欲,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌陋率,老刑警劉巖球化,帶你破解...
    沈念sama閱讀 219,110評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異瓦糟,居然都是意外死亡筒愚,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,443評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門狸页,熙熙樓的掌柜王于貴愁眉苦臉地迎上來锨能,“玉大人,你說我怎么就攤上這事芍耘≈酚觯” “怎么了?”我有些...
    開封第一講書人閱讀 165,474評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵斋竞,是天一觀的道長(zhǎng)倔约。 經(jīng)常有香客問我,道長(zhǎng)坝初,這世上最難降的妖魔是什么浸剩? 我笑而不...
    開封第一講書人閱讀 58,881評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮鳄袍,結(jié)果婚禮上绢要,老公的妹妹穿的比我還像新娘。我一直安慰自己拗小,他們只是感情好重罪,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,902評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般剿配。 火紅的嫁衣襯著肌膚如雪搅幅。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,698評(píng)論 1 305
  • 那天呼胚,我揣著相機(jī)與錄音茄唐,去河邊找鬼。 笑死蝇更,一個(gè)胖子當(dāng)著我的面吹牛沪编,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播簿寂,決...
    沈念sama閱讀 40,418評(píng)論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼漾抬,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了常遂?” 一聲冷哼從身側(cè)響起纳令,我...
    開封第一講書人閱讀 39,332評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎克胳,沒想到半個(gè)月后平绩,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,796評(píng)論 1 316
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡漠另,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,968評(píng)論 3 337
  • 正文 我和宋清朗相戀三年捏雌,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片笆搓。...
    茶點(diǎn)故事閱讀 40,110評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡性湿,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出满败,到底是詐尸還是另有隱情肤频,我是刑警寧澤,帶...
    沈念sama閱讀 35,792評(píng)論 5 346
  • 正文 年R本政府宣布算墨,位于F島的核電站宵荒,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏净嘀。R本人自食惡果不足惜报咳,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,455評(píng)論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望挖藏。 院中可真熱鬧暑刃,春花似錦、人聲如沸膜眠。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,003評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至婿脸,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間柄驻,已是汗流浹背狐树。 一陣腳步聲響...
    開封第一講書人閱讀 33,130評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留鸿脓,地道東北人抑钟。 一個(gè)月前我還...
    沈念sama閱讀 48,348評(píng)論 3 373
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像野哭,于是被迫代替她去往敵國(guó)和親在塔。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,047評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容