UINavigationBar UINavigationItem

//UINavigationBar导航条:(320*44) UINavigationItem决定导航条的显示   

//320*480的点坐标系, 4、4s中像素的值:640*960,但不影响点坐标的计算
    //UINavigationBar 导航条:(320*44)
    //所有vc共用一个导航条
    //self.navigationController.navigationBar
    //iOS7之后,self.view原点默认在屏幕的左上角,导航条默认为半透明的
    //将半透明属性设置为NO,view的原点从导航条的下边算起
    self.navigationController.navigationBar.translucent = YES;
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn setFrame:CGRectMake(10,74,300,30)];
    [btn setTitle:@"push" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
    btn.backgroundColor = [UIColor redColor];
    [self.view addSubview:btn];
    self.view.backgroundColor = [UIColor yellowColor];
    
    //为导航条贴图
    //UIBarMetricsDefault 对应iPhone竖屏的状态,人像模式
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBg.png"] forBarMetrics:UIBarMetricsDefault];
    //设置iPhone处于横屏状态,导航条的图片
    //UIBarMetricsLandscapePhone iPhone横屏状态,风景模式
    //横屏:(480*32)
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBg-32.png"] forBarMetrics:UIBarMetricsLandscapePhone];
    
    
    //UINavigationItem,决定导航条的显示
    //UINavigationBar 有一个栈来维护UINavigationItem对象,我们对vc入栈时,导航条会自动push一个UINavigationItem对象
    //vc与显示在导航条上的Item对象一一对应
    //设置标题
    self.navigationItem.title = @"root";
    //设置标题视图(应用最多的是贴上公司的logo)
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,30)];
    view.backgroundColor = [UIColor orangeColor];
    self.navigationItem.titleView = view;
    
    //UIBarButtonItem 显示在导航条或者工具栏上的按钮
    //通过设置标题得到UIBarButtonItem对象
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"left" style:UIBarButtonItemStylePlain target:self action:@selector(leftItemClicked)];
    self.navigationItem.leftBarButtonItem = item1;
    //利用系统预置的样式创建
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(rightItemClicked:)];
    item2.tag = 100;
    //self.navigationItem.rightBarButtonItem = item2;
    //最重要,最常用的创建方式:(传入自定义视图的创建方式)
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0,0,20,20);
    [btn setBackgroundImage:[UIImage imageNamed:@"itemImage.png"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithCustomView:btn];
    item3.tag = 101;
    NSArray *itemArray = [NSArray arrayWithObjects:item2,item3,nil];
    //设置一组按钮
    self.navigationItem.rightBarButtonItems = itemArray;
    
    //显示隐藏的工具栏:(UIToolBar 320*44),所有vc共用一个工具栏
    [self.navigationController setToolbarHidden:NO animated:YES];
    //为工具栏贴图
    //UIBarPositionBottom 任何一个UIView都可以设置topBar和BottomBar
    //[self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"toolBar.png"] forToolbarPosition:UIBarPositionBottom barMetrics:UIBarMetricsDefault];
    
     //设置一组自动间隔的按钮
     UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(toolItemClicked)];
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(toolItemClicked)];
    //UIBarButtonSystemItemFlexibleSpace 自动计算间隔的按钮
    //UIBarButtonSystemItemFixedSpace width 我们自己设定
    UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    //spaceItem.width
    NSArray *array = [NSArray arrayWithObjects:spaceItem,item1,spaceItem,item2,spaceItem,nil];
    self.toolbarItems = array;
    
//替代默认的back按钮
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"custom" style:UIBarButtonItemStylePlain target:self action:@selector(leftItemClicked)];
    self.navigationItem.leftBarButtonItem = item;    
    
    
    【UIBarButtonItem】
//导航条专用按钮

//四种创建方式
- (id)initWithImage:(UIImage *)image style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action;
- (id)initWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action;
- (id)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem target:(id)target action:(SEL)action;
- (id)initWithCustomView:(UIView *)customView;

UIBarButtonSystemItemFlexibleSpace,是自带的一种空格可以当作占位符用


【self.navigationItem】
//给导航条设置专用按钮和titleView

//隐藏自带的返回按钮
@property(nonatomic,assign) BOOL hidesBackButton;
- (void)setHidesBackButton:(BOOL)hidesBackButton animated:(BOOL)animated;

//中间的view
@property(nonatomic,retain) UIView *titleView;


//左边和右边的专用按钮
@property(nonatomic,retain) UIBarButtonItem *leftBarButtonItem;
@property(nonatomic,retain) UIBarButtonItem *rightBarButtonItem;
- (void)setLeftBarButtonItem:(UIBarButtonItem *)item animated:(BOOL)animated;
- (void)setRightBarButtonItem:(UIBarButtonItem *)item animated:(BOOL)animated;


//左边和右边的专用按钮组(数组中存放的是UIBarButtonItem)
@property(nonatomic,copy) NSArray *leftBarButtonItems;
@property(nonatomic,copy) NSArray *rightBarButtonItems;
- (void)setLeftBarButtonItems:(NSArray *)items animated:(BOOL)animated; 
- (void)setRightBarButtonItems:(NSArray *)items animated:(BOOL)animated;



//【UIToolbar】工具条
//每个导航控制器都自带有一个工具栏,是一个UIToolBar的对象,显示在最底部,尺寸(320*44),工具栏默认处于隐藏状态
//显示工具栏
[self.navigationController setToolbarHidden:NO];
//风格
@property(nonatomic,assign) UIBarStyle barStyle;
//是否半透明
@property(nonatomic,assign,getter=isTranslucent) BOOL translucent; // Default is NO on iOS 6 and earlier. Always YES if barStyle is set to UIBarStyleBlackTranslucent
//内容渲染颜色
@property(nonatomic,retain) UIColor *tintColor;
//导航条的背景色
@property(nonatomic,retain) UIColor *barTintColor;
//【上面4个属性和navigationBar的使用方式一样】

//设置背景图片
- (void)setBackgroundImage:(UIImage *)backgroundImage forToolbarPosition:(UIBarPosition)topOrBottom barMetrics:(UIBarMetrics)barMetrics;
//UIBarPosition中的枚举值只支持iOS7
//UIToolbarPositionBottom(在iOS7和7以前的操作系统都适用)
//UIToolbarPositionBottom 理解(每个UIView都可以指定TopBar 和BottomBar),导航控制器中工具栏是作为导航控制器view的BottomBar被自动创建出来的

//给toolBar设置专用button(数组中也是UIBarButtonItem)
//让viewController直接设置
//self.toolbarItems = array;
@property (nonatomic, retain) NSArray *toolbarItems;
- (void)setToolbarItems:(NSArray *)toolbarItems animated:(BOOL)animated;
原文地址:https://www.cnblogs.com/liudongyan/p/4399284.html