UINavigationItem
UINavigationItem 常用属性和方法:
UINavigationItem 初始化方法:
//
// RootViewController.m
// NavigetionBarDemo
//
// Created by ccy on 13-12-29.
// Copyright (c) 2013年 ccy. All rights reserved.
//
#import "RootViewController.h"
#import "SecondViewController.h"
@interface RootViewController ()
{
SecondViewController * _secondVC;
}
@end
@implementation RootViewController
- (void)dealloc
{
[_secondVC release];
[super dealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor greenColor];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(10, 30, 300, 30);
[btn setTitle:@"second view" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnclick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
//标题
//[self.navigationItem setTitle:@"root"];
self.navigationItem.title = @"root";
//标题视图 CGRectMake(100, 50, 100, 40);这个没有起作用,[视图居中显示]
UIView *view = [[UIView alloc] init];
view.frame = CGRectMake(100, 50, 100, 40);
view.backgroundColor = [UIColor brownColor];
self.navigationItem.titleView = view;
[view release];
/**************************************/
//初始化导航条左按键标题
//导航条风格
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
/*
UIBarButtonItemStylePlain, - 按键颜色和导航条颜色一样
UIBarButtonItemStyleBordered,
UIBarButtonItemStyleDone, - 按键颜色为蓝色
*/
UIBarButtonItem * item1 = [[UIBarButtonItem alloc] initWithTitle:@"左侧" style:UIBarButtonItemStyleBordered target:self action:@selector(itemClick:)];
self.navigationItem.leftBarButtonItem = item1;
[item1 release];
/**************************************/
//接收一个数组
UIBarButtonItem * item2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(itemClick:)];
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// x,y 数值没用
btn1.frame = CGRectMake(0 , 0, 50, 30);
[btn1 setTitle:@"btn1" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnclick) forControlEvents:UIControlEventTouchUpInside];
//初始化 接收一个按键
UIBarButtonItem * item3 = [[UIBarButtonItem alloc] initWithCustomView:btn1];
//添加一个图片控键[最适合的图片大小是40x40]
UIBarButtonItem * item4 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"itemImage.png"] style:UIBarButtonItemStylePlain target:self action:@selector(itemClick:)];
NSArray * itemArray = [NSArray arrayWithObjects:item2,item3,item4, nil];
[item2 release];
[item3 release];
//将 数组加入导航栏
self.navigationItem.rightBarButtonItems = itemArray;
_secondVC = [[SecondViewController alloc] init];
}
- (void)itemClick:(id)sender
{
self.view.backgroundColor = [UIColor redColor];
}
- (void)btnclick
{
[self.navigationController pushViewController:_secondVC animated:YES];
}
//
// SecondViewController.m
// NavigetionBarDemo
//
// Created by ccy on 13-12-29.
// Copyright (c) 2013年 ccy. All rights reserved.
//
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor redColor];
[self.navigationItem setTitle:@"second"];
/**************************************/
//覆盖它的back按钮
UIBarButtonItem * item1 = [[UIBarButtonItem alloc] initWithTitle:@"后退" style:UIBarButtonItemStyleBordered target:self action:@selector(itemClick:)];
self.navigationItem.leftBarButtonItem = item1;
// [item1 release];
}
- (void) itemClick:(id)sender
{
[self.navigationController popToRootViewControllerAnimated:YES];
}