UINavigationController特性總結(jié)(2)

1.navigationItem設(shè)置

navigationItem可以設(shè)置:

屬性 效果
title 標(biāo)題
titleView 標(biāo)題視圖(UIView)
leftBarButtonItem 左邊的按鈕(UIBarButtonItem)
leftBarButtonItems 左邊的按鈕們(UIBarButtonItem數(shù)組)
rightBarButtonItem 右邊的按鈕
rightBarButtonItems 右邊的按鈕們
backBarButonItem 返回按鈕(UIBarButtonItem)
prompt 描述

1.1 title/leftBarButtonItem/rightBarButtonItems設(shè)置

#import "ViewController.h"
@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor orangeColor]];
    
    self.navigationController.navigationBar.translucent = NO;

    CGFloat w = [UIScreen mainScreen].bounds.size.width;
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(8, 0, w-16, 220)];
    imageView.image = [UIImage imageNamed:@"hy.jpg"];
    [self.view addSubview:imageView];
    
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"item1" style:UIBarButtonItemStylePlain target:self action:@selector(btnClicked:)];
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"item2" style:UIBarButtonItemStylePlain target:self action:@selector(btnClicked:)];
    self.navigationItem.title = @"Hello World";
    self.navigationItem.leftBarButtonItem = item1;
    self.navigationItem.rightBarButtonItems = @[item1,item2];
    
    //leftItems/rightItems的顏色
    self.navigationController.navigationBar.tintColor = [UIColor redColor];
    //設(shè)置NavigationBar背景顏色
    self.navigationController.navigationBar.barTintColor = [UIColor greenColor];
    //設(shè)置title的文字顏色
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor yellowColor]};

需要注意的是 navigationController和 navigationBar和 navigationItem三者的關(guān)系,這一點(diǎn)在下一小節(jié)論述.
還有,要記得如何設(shè)置title的顏色,如何設(shè)置items的顏色,如何設(shè)置navigationBar的顏色.

效果:
效果.png

1.2 prompt設(shè)置

#import "ViewController.h"
@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor orangeColor]];
    
    self.navigationController.navigationBar.translucent = NO;

    CGFloat w = [UIScreen mainScreen].bounds.size.width;
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(8, 0, w-16, 220)];
    imageView.image = [UIImage imageNamed:@"hy.jpg"];
    [self.view addSubview:imageView];

    self.navigationItem.title = @"Hello World";
 
    //描述信息,navigationBar高度增加30,由44變成74
    self.navigationItem.prompt = @"this is prompt";
    
}

prompt屬性用來添加描述信息,賦值后,navigationBar高度增加30,由44變成74

效果
效果

1.3 titleView設(shè)置

#import "ViewController.h"
@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor orangeColor]];
    
    self.navigationController.navigationBar.translucent = NO;
 
    CGFloat w = [UIScreen mainScreen].bounds.size.width;
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(8, 0, w-16, 220)];
    imageView.image = [UIImage imageNamed:@"hy.jpg"];
    [self.view addSubview:imageView];

    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    UILabel *titleLabe = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    titleLabe.text = @"newtitle";
    titleLabe.textAlignment = NSTextAlignmentCenter;
    titleLabe.textColor = [UIColor redColor];
    [titleView addSubview:titleLabe];
    titleView.backgroundColor = [UIColor yellowColor];
    self.navigationItem.titleView = titleView;
}
注意

如果需要在 self.navigationItem.titleView 中添加子控件,需要先實(shí)例化一次self.navigationItem.titleView:
self.navigationItem.titleView = [[UIView alloc] init];
不然無論你忘titleView中放多少控件,都是顯示不出來的.

效果
效果

1.4 自定義返回按鈕

-(void)btnClicked:(UIBarButtonItem *)sender{
    UIViewController *vc = [[UIViewController alloc] init];
    vc.view.backgroundColor = [UIColor purpleColor];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"自定義返回" style:UIBarButtonItemStylePlain target:vc action:nil];
    vc.navigationItem.leftBarButtonItem = backItem;
    [self.navigationController pushViewController:vc animated:YES];
}

自定義返回按鈕,最簡單的方法就是,設(shè)置push出了的新控制器的leftBarButtonItem,當(dāng)leftBarButtonItem設(shè)置好之后, backBarButonItem就不顯示了.


效果

2. navigationBar和 navigationItem的關(guān)系

(該小節(jié)參考:http://blog.csdn.net/luoyeffcs/article/details/16106707/)
先看一下這些類的繼承關(guān)系,可以看清很多問題.

繼承自
UINavigationController UIViewController
UINavigationBar UIView
UINavigationItem NSObject
UIBarButtonItem UIBarItem
UIBarItem NSObject
問題1

navigationItem和navigationController都是UIViewController的屬性,而UINavigationController的父類就是UIViewController,所以會出現(xiàn):
self.navigationController.navigationController
self.navigationController.navigationItem這種現(xiàn)象(self指UIViewController).而這應(yīng)該是被忽略的屬性(沒啥亂用,容易混淆).

navigationItem是UIViewController的屬性,
正確設(shè)置navigationItem:self. navigationItem
navigationBar是UINavigationController的屬性,
正確設(shè)置navigationBar :self.navigationController.navigationBar

問題2

self.title 和self.navigationItem.title 和self.tabBarItem.title
由于UIViewController本身包含navigationItem和tabBarItem飘言,所以可以單獨(dú)對其title賦值碍扔,self.title會重寫另外兩個(gè)的值难裆,只是提供的一種便利方法。

當(dāng)self.title = @"123"時(shí), self.navigationItem.title 的值和 self.tabBarItem.title 的值都是@"123"

問題3

UIBarItem和 UIBarButtonItem 還有 UINavigationItem的關(guān)系
UIBarItem類是一個(gè)可以放置在Bar之上的所有小控件類的抽象類沉眶。繼承了該基類所有子類在外觀上類似于一個(gè)Button,它們都有一個(gè)標(biāo)題安券,圖片悉盆,動作以及目標(biāo).
** UIBarButtonItem繼承自UIBarItem,專門用來放在UIToolbar 或者 UINavigationBar的特殊button.基本行為跟button是一樣的。
** UINavigationItem
包含了當(dāng)前頁面導(dǎo)航欄上需要顯示的全部信息,包括:
title, prompt, titleView, leftBarButtonItem, leftBarButtonItems, rightBarButtonItem, rightBarButtonItems, backBarButonItem

問題4

navigationBar的各種顏色設(shè)置

//設(shè)置標(biāo)題
self.navigationItem.title = @"Hello World";
//設(shè)置左邊的按鈕
self.navigationItem.leftBarButtonItem = item1;
//設(shè)置右面的按鈕 們
self.navigationItem.rightBarButtonItems = @[item1,item2];

//設(shè)置leftItems/rightItems的顏色
self.navigationController.navigationBar.tintColor = [UIColor redColor];
//設(shè)置NavigationBar背景顏色
self.navigationController.navigationBar.barTintColor = [UIColor greenColor];
//設(shè)置NavigationBar背景圖片
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
//設(shè)置title的文字顏色
self.navigationController.navigationBar.titleTextAttributes @{NSForegroundColorAttributeName:[UIColor yellowColor]};
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末典唇,一起剝皮案震驚了整個(gè)濱河市镊折,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌介衔,老刑警劉巖恨胚,帶你破解...
    沈念sama閱讀 217,185評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異炎咖,居然都是意外死亡赃泡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評論 3 393
  • 文/潘曉璐 我一進(jìn)店門乘盼,熙熙樓的掌柜王于貴愁眉苦臉地迎上來升熊,“玉大人,你說我怎么就攤上這事绸栅×潘椋” “怎么了?”我有些...
    開封第一講書人閱讀 163,524評論 0 353
  • 文/不壞的土叔 我叫張陵阴幌,是天一觀的道長勺阐。 經(jīng)常有香客問我,道長矛双,這世上最難降的妖魔是什么渊抽? 我笑而不...
    開封第一講書人閱讀 58,339評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮议忽,結(jié)果婚禮上懒闷,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好愤估,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,387評論 6 391
  • 文/花漫 我一把揭開白布帮辟。 她就那樣靜靜地躺著,像睡著了一般玩焰。 火紅的嫁衣襯著肌膚如雪由驹。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,287評論 1 301
  • 那天昔园,我揣著相機(jī)與錄音蔓榄,去河邊找鬼。 笑死默刚,一個(gè)胖子當(dāng)著我的面吹牛甥郑,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播荤西,決...
    沈念sama閱讀 40,130評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼澜搅,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了邪锌?” 一聲冷哼從身側(cè)響起勉躺,我...
    開封第一講書人閱讀 38,985評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎秃流,沒想到半個(gè)月后赂蕴,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體柳弄,經(jīng)...
    沈念sama閱讀 45,420評論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡舶胀,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,617評論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了碧注。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片嚣伐。...
    茶點(diǎn)故事閱讀 39,779評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖萍丐,靈堂內(nèi)的尸體忽然破棺而出轩端,到底是詐尸還是另有隱情,我是刑警寧澤逝变,帶...
    沈念sama閱讀 35,477評論 5 345
  • 正文 年R本政府宣布基茵,位于F島的核電站,受9級特大地震影響壳影,放射性物質(zhì)發(fā)生泄漏拱层。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,088評論 3 328
  • 文/蒙蒙 一宴咧、第九天 我趴在偏房一處隱蔽的房頂上張望根灯。 院中可真熱鬧,春花似錦、人聲如沸烙肺。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,716評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽桃笙。三九已至氏堤,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間怎栽,已是汗流浹背丽猬。 一陣腳步聲響...
    開封第一講書人閱讀 32,857評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留熏瞄,地道東北人脚祟。 一個(gè)月前我還...
    沈念sama閱讀 47,876評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像强饮,于是被迫代替她去往敵國和親由桌。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,700評論 2 354

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

  • (一)UINavigationController及其相關(guān)控件之間的關(guān)系 通過對上述幾個(gè)類的屬性的羅列邮丰,我們可以做...
    MrJ的雜貨鋪閱讀 20,647評論 2 65
  • 1.自定義控件 a.繼承某個(gè)控件 b.重寫initWithFrame方法可以設(shè)置一些它的屬性 c.在layouts...
    圍繞的城閱讀 3,389評論 2 4
  • { 11行您、核心動畫 需要簽協(xié)議,但是系統(tǒng)幫簽好 一剪廉、CABasicAnimation 1娃循、創(chuàng)建基礎(chǔ)動畫對象 CAB...
    CYC666閱讀 1,545評論 2 4
  • 1,Search Bar 怎樣去掉背景的顏色(storyboard里只能設(shè)置background顏色,可是發(fā)現(xiàn)cl...
    以德扶人閱讀 2,350評論 2 50
  • 今晚店里來了兩個(gè)中國女人,一個(gè)年輕的一個(gè)年長的泉沾,一點(diǎn)日語都不會捞蚂,用機(jī)器買飯也不會,我給她們講解然后幫忙投幣買的跷究,日...
    雁兒與安閱讀 282評論 0 1