AppDelegate.m
#import "RootViewController.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
RootViewController *rootCtrl = [[RootViewController alloc] init];
//创建一个导航控制器
UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:rootCtrl];
navCtrl.delegate = self;
self.window.rootViewController = navCtrl;
return YES;
}
#pragma mark - UINavigationController delegate
//子控制器的视图将要显示的时候调用的代理方法
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
NSLog(@"willShow:viewCtrl:%@",viewController);
}
//子控制器的视图已经显示的时候调用的代理方法
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
NSLog(@"didShow:viewCtrl:%@",viewController);
}
RootViewController.m
#import "SecondViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(30, 20, 90, 90)];
view.backgroundColor = [UIColor redColor];
[self.view addSubview:view];
/*___________________________<span style="color:#ff0000;">navigationItem设置标题或者标题视图</span>_____________________________*/
// self.title = @"自定义标题";
//注意:self.navigationItem获取当前的导航向
// self.navigationItem.title = @"自定义视图";
//自定义视图作为标题试图
// UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 44)];
// label.text = @"自定义视图label";
// label.textColor = [UIColor redColor];
// self.navigationItem.titleView = label;
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"标题按钮" forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 100, 30);
button.backgroundColor = [UIColor greenColor];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.titleView = button;
/*___________________________<span style="color:#cc0000;">navigationItem两侧的按钮</span>_____________________________*/
//导航栏的左侧按钮设置
//1.方式一:
UIBarButtonItem *leftItem1 = [[UIBarButtonItem alloc] initWithTitle:@"menu" style:UIBarButtonItemStyleDone target:self action:@selector(leftButtonAction)];
// self.navigationItem.leftBarButtonItem = leftItem1;
//2.方式二:
UIBarButtonItem *leftItem2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addAction)];
// self.navigationItem.leftBarButtonItem = leftItem2;
//在左侧同时显示多个按钮
self.navigationItem.leftBarButtonItems = @[leftItem1,leftItem2];
//3.方式三:自定义视图
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(0, 0, 68, 33);
[button1 setTitle:@"右侧" forState:UIControlStateNormal];
[button1 setBackgroundImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
[button1 setBackgroundImage:[UIImage imageNamed:@"back_on"] forState:UIControlStateHighlighted];
[button1 addTarget:self action:@selector(rightButtonAction) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button1];
self.navigationItem.rightBarButtonItem = item;
/*
在右侧添加多个按钮的方式:(1) self.navigationItem.rightBarButtonItems
(2) 自定义视图
*/
//设置下一个视图控制器的返回按钮的标题
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"首页" style:UIBarButtonItemStyleDone target:nil action:nil];
self.navigationItem.backBarButtonItem = backItem;
/*_____________________<span style="background-color: rgb(255, 0, 0);">navigationBar的背景设置</span>________________________*/
//设置导航栏的显示样式:不会改变他的零点
/*
UIBarStyleBlack:黑色的
UIBarStyleBlackTranslucent:黑色透明
*/
// self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
//设置导航栏的背景颜色
//这种方法在iOS7中不可行
// self.navigationController.navigationBar.tintColor = [UIColor greenColor];
//在iOS7中使用下面这种方式,iOS6中不可以使用
// self.navigationController.navigationBar.barTintColor = [UIColor greenColor];
//使用图片设置导航栏的背景颜色
/*
UIBarMetricsLandscapePhone:横屏显示的样式
*/
//如果我们使用图片设置导航栏的背景,则零点会下移到导航栏的下侧
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar_bg_normal1"] forBarMetrics:UIBarMetricsDefault];
//设置在iOS7的时候导航栏不穿透
self.edgesForExtendedLayout = UIRectEdgeNone;
//设置导航栏不透明
// self.navigationController.navigationBar.translucent = NO;
//将图片设置成一个视图的背景颜色
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"1"]];
}
- (void)rightButtonAction {
NSLog(@"右侧按钮被点击了");
}
- (void)buttonAction:(UIButton *)button {
self.navigationItem.prompt = @"<span style="color:#ff0000;">正在保存...</span>";
//延迟2秒关闭
[self.navigationItem performSelector:@selector(setPrompt:) withObject:nil afterDelay:2];
}
- (void)leftButtonAction {
NSLog(@"左侧按钮被点击了");
}
- (void)addAction {
SecondViewController *secondCtrl = [[SecondViewController alloc] init];
[self.navigationController pushViewController:secondCtrl animated:YES];
}
SecondViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
//隐藏返回按钮
self.navigationItem.hidesBackButton = YES;
//自定义返回按钮
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 68, 33);
button.showsTouchWhenHighlighted = YES;
[button setTitle:@"返回" forState:UIControlStateNormal];
//设置背景图片
[button setBackgroundImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"back_on"] forState:UIControlStateHighlighted];
[button addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = leftItem;
//打开工具栏
// [self.navigationController setToolbarHidden:NO];
[self.navigationController setToolbarHidden:NO animated:YES];
}
- (void)backAction {
[self.navigationController popViewControllerAnimated:YES];
}