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

1.navigationItem設(shè)置

navigationItem可以設(shè)置:

屬性 效果
title 標題
titleView 標題視圖(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)系,這一點在下一小節(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 中添加子控件,需要先實例化一次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,當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,所以可以單獨對其title賦值,self.title會重寫另外兩個的值,只是提供的一種便利方法。

當self.title = @"123"時, self.navigationItem.title 的值和 self.tabBarItem.title 的值都是@"123"

問題3

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

問題4

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

//設(shè)置標題
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)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

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

友情鏈接更多精彩內(nèi)容