自定義 NavigationBar的一系列屬性

效果圖 Gif

1.自定義leftBarButtonItem , 自定義 RigthBarButtonItem , 自定義中間的 titleView ;

2.修改返回按鈕到左邊的寬度太大的問(wèn)題:(UIBarButtonSystemItemFixedSpace)
我單獨(dú)設(shè)置出一個(gè)空 button 來(lái)占位,并且寬度設(shè)置為-20 , 這樣數(shù)組中第二個(gè)返回按鈕的位置就會(huì)相應(yīng)的往左側(cè)移動(dòng)了!
UIBarButtonItem *negSpaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];

3.leftBackItem 與 closeItem 的寬度是改不了的~因?yàn)樗麄兊膶挾仁歉鶕?jù) image 自適應(yīng)的 , 所以修改他們之間的距離不應(yīng)該從 width 屬性來(lái)修改 , 應(yīng)該在 UI 切圖的時(shí)候去審慎定奪;

效果圖 Gif.gif

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //全局統(tǒng)一設(shè)置:
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navBack"] forBarMetrics:UIBarMetricsDefault];
    [[UINavigationBar appearance] setTintColor:[UIColor redColor]];
    UIImage *backImage = [[UIImage imageNamed:@"imgBack"] imageWithAlignmentRectInsets:UIEdgeInsetsMake(0, 0, -3, 0)];
    //返回箭頭王上挪動(dòng)3;
    [[UINavigationBar appearance] setBackIndicatorImage:backImage];
    [[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:backImage];
    //或者:讓箭頭右側(cè)的文字網(wǎng)上走一點(diǎn)兒:負(fù)數(shù)向上移動(dòng);
//    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -1.5f) forBarMetrics:UIBarMetricsDefault];
    
    return YES;
}

ViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"第一頁(yè)";
    
//    [self.navigationController.navigationBar setTranslucent:NO];
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBack"] forBarMetrics:UIBarMetricsDefault];
//    self.navigationController.navigationBar.backgroundColor = [UIColor grayColor];
//    [self.navigationController.navigationBar setBarTintColor:[UIColor blackColor]];
//    self.view.backgroundColor = [UIColor blueColor];
//    [self.navigationController.navigationBar setTranslucent:NO];
//    self.navigationController.navigationBar.clipsToBounds = YES;
    //替換分割線的時(shí)候前提是背景圖也要被替換才行:(setBackgroundImage: forBarMetrics:)這個(gè)方法:
//    [self.navigationController.navigationBar setShadowImage:[UIImage imageNamed:@"navSep"]];
//    
//    //NSShadowAttributeName:
//    NSShadow *shadow = [NSShadow new];
//    [shadow setShadowColor:[UIColor colorWithWhite:0.0f alpha:0.75f]];
//    [shadow setShadowOffset:CGSizeMake(1.0f, 1.0f)];
//    [shadow setShadowBlurRadius:1.0f];
//    
//    NSDictionary *navBarTitleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor yellowColor], NSForegroundColorAttributeName, shadow, NSShadowAttributeName, nil];
//    //富文本定義標(biāo)題:
//    [self.navigationController.navigationBar setTitleTextAttributes:navBarTitleTextAttributes];
//    
//    
//    //標(biāo)題垂直偏移:
//    [self.navigationController.navigationBar setTitleVerticalPositionAdjustment:10.0f forBarMetrics:UIBarMetricsDefault];
//    
//    //修改返回按鈕的顏色:
//    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
//    //修改返回按鈕圖片:(這兩個(gè)要同時(shí)設(shè)置);
//    [self.navigationController.navigationBar setBackIndicatorImage:[UIImage imageNamed:@"imgBack"]];
//    [self.navigationController.navigationBar setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"imgBack"]];
//    //隱藏:
//    [self.navigationController.navigationBar setHidden:YES];
    
    //View controller-based status bar appearance 設(shè)置為 NO:
//    [self.navigationController.navigationBar setBarTintColor:[UIColor blackColor]];
    //不在推薦使用的方式:
//    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    
}

NextViewController.m

#import "NextViewController.h"

@interface NextViewController ()

@end

@implementation NextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"第二頁(yè)";
    self.view.backgroundColor = [UIColor whiteColor];
    
//    UIBarButtonItem *leftBackItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"imgBack"] style:UIBarButtonItemStylePlain target:self action:@selector(goBack)];
//    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
//    UIBarButtonItem *closeItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"navClose"] style:UIBarButtonItemStylePlain target:self action:@selector(closeAction)];
    //修改返回按鈕到左邊的寬度太大的問(wèn)題:(UIBarButtonSystemItemFixedSpace)
    //我單獨(dú)設(shè)置出一個(gè)空 button 來(lái)占位,并且寬度設(shè)置為-20 , 這樣數(shù)組中第二個(gè)返回按鈕的位置就會(huì)相應(yīng)的往左側(cè)移動(dòng)了!
//    UIBarButtonItem *negSpaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
//    negSpaceItem.width = -10;
    //leftBackItem 與 closeItem 的寬度是改不了的~因?yàn)樗麄兊膶挾仁歉鶕?jù) image 自適應(yīng)的 , 所以修改他們之間的距離不應(yīng)該從 width 屬性來(lái)修改 , 應(yīng)該在 UI 切圖的時(shí)候去審慎定奪;
    
    //右側(cè)分享按鈕:
//    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"navShare"] style:UIBarButtonItemStylePlain target:self action:@selector(shareAction)];
//    UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 44)];
//    customView.backgroundColor = [UIColor yellowColor];
//    UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithCustomView:customView];
//    
//    self.navigationItem.leftBarButtonItems = @[negSpaceItem, leftBackItem, customItem,closeItem];
//    self.navigationItem.rightBarButtonItems = @[rightItem];
//    
//    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
//    titleView.backgroundColor = [UIColor cyanColor];
//    self.navigationItem.titleView = titleView;
    
    
}


- (void)goBack {
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)closeAction {
    [self.navigationController popViewControllerAnimated:YES];
    NSLog(@"點(diǎn)擊關(guān)閉按鈕");
}

- (void)shareAction {
    [self.navigationController popViewControllerAnimated:YES];
    NSLog(@"點(diǎn)擊分享按鈕");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end

愿編程讓這個(gè)世界更美好

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末呕屎,一起剝皮案震驚了整個(gè)濱河市典唇,隨后出現(xiàn)的幾起案子斑唬,更是在濱河造成了極大的恐慌,老刑警劉巖族檬,帶你破解...
    沈念sama閱讀 217,907評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件耿焊,死亡現(xiàn)場(chǎng)離奇詭異纽竣,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)距潘,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,987評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)只搁,“玉大人音比,你說(shuō)我怎么就攤上這事⌒胛希” “怎么了硅确?”我有些...
    開(kāi)封第一講書人閱讀 164,298評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)明肮。 經(jīng)常有香客問(wèn)我菱农,道長(zhǎng),這世上最難降的妖魔是什么柿估? 我笑而不...
    開(kāi)封第一講書人閱讀 58,586評(píng)論 1 293
  • 正文 為了忘掉前任循未,我火速辦了婚禮,結(jié)果婚禮上秫舌,老公的妹妹穿的比我還像新娘的妖。我一直安慰自己,他們只是感情好足陨,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,633評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布嫂粟。 她就那樣靜靜地躺著,像睡著了一般墨缘。 火紅的嫁衣襯著肌膚如雪星虹。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書人閱讀 51,488評(píng)論 1 302
  • 那天镊讼,我揣著相機(jī)與錄音宽涌,去河邊找鬼。 笑死蝶棋,一個(gè)胖子當(dāng)著我的面吹牛卸亮,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播玩裙,決...
    沈念sama閱讀 40,275評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼兼贸,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼段直!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起寝受,我...
    開(kāi)封第一講書人閱讀 39,176評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤坷牛,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后很澄,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體京闰,經(jīng)...
    沈念sama閱讀 45,619評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,819評(píng)論 3 336
  • 正文 我和宋清朗相戀三年甩苛,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了蹂楣。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,932評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡讯蒲,死狀恐怖痊土,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情墨林,我是刑警寧澤赁酝,帶...
    沈念sama閱讀 35,655評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站旭等,受9級(jí)特大地震影響酌呆,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜搔耕,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,265評(píng)論 3 329
  • 文/蒙蒙 一隙袁、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧弃榨,春花似錦菩收、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 31,871評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至官辈,卻和暖如春箱舞,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背钧萍。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 32,994評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留政鼠,地道東北人风瘦。 一個(gè)月前我還...
    沈念sama閱讀 48,095評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像公般,于是被迫代替她去往敵國(guó)和親万搔。 傳聞我的和親對(duì)象是個(gè)殘疾皇子胡桨,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,884評(píng)論 2 354

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