UINavigationController

AppDelegate.m

//创建window

//20状态栏+ 44导航栏

self.window= [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

self.window.backgroundColor= [UIColor whiteColor];

[self.window makeKeyAndVisible];

导航栏


//初始化视图控制器

//名字要一致NowViewController

NowViewController * vc = [[NowViewController alloc] init];

//导航控制器把vc设置为导航控制器的根视图

UINavigationController * Nav = [[UINavigationController alloc] initWithRootViewController:vc];

//把导航控制器赋值给当前window的跟视图以此来管理放在导航控制器上的视图(ViewController)

self.window.rootViewController = Nav;

//当前导航标题要设置在"当前"

//[vc setTitle:@"first"];


NowViewController.m

//当前导航标题要设置在"当前"

self.title=@"first";

UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(10, 100, 60, 30)];

[btn setTitle:@"push" forState:UIControlStateNormal];

btn.backgroundColor= [UIColorblueColor];

btn.layer.cornerRadius= 10;

[self.view addSubview:btn];

[btn addTarget:self action:@selector(push) forControlEvents:UIControlEventTouchUpInside];

}

//视图将要出现

-(void)viewWillDisappear:(BOOL)animated {

//yes

self.navigationController.navigationBarHidden=NO;

}

//视图将要消失的方法

//-(void)viewWillDisappear:(BOOL)animated {

//    //no

//    self.navigationController.navigationBarHidden = NO;

//}

- (void)push {

//视图控制器的跳转方式:

//1.导航:压栈出栈

//2.模态:弹出消失

NowViewController1 * vc = [[NowViewController1 alloc] init];

//压栈

[self.navigationController pushViewController:vc animated:YES];

//[vc setTitle:@"second"];

}

NowViewController1.m

self.title=@"second";

//解决push的时候会有1s左右的灰色阴影

self.view.backgroundColor= [UIColor whiteColor];

//隐藏导航标题。导航栏隐藏属性如果当前页面消失的时候没有设置为no的话,那么在下一级页面也是隐藏的:解决办法:在当前页面将要消失的方法中把导航栏隐藏属性设置为no

self.navigationController.navigationBarHidden=NO;

//出栈按钮

UIButton * pop = [[UIButton alloc] initWithFrame:CGRectMake(10, 100, 60, 30)];

pop.backgroundColor= [UIColororangeColor];

[pop setTitle:@"pop" forState:UIControlStateNormal];

pop.layer.cornerRadius= 10;

[pop addTarget:self action:@selector(pop) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:pop];

UIButton * btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 60, 30)];

btn.backgroundColor= [UIColor orangeColor];

[btn setTitle:@"push" forState:UIControlStateNormal];

btn.layer.cornerRadius= 10;

[btn addTarget:selfaction:@selector(push) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btn];

}

- (void)pop {

//出栈

//出栈到根视图控制器

//[self.navigationController popToRootViewControllerAnimated:YES];

//回到上一级页面:(导航中的页面)

[self.navigationController popViewControllerAnimated:YES];

}

- (void)push {

NowViewController2 * third = [[NowViewController2 alloc] init];

[self.navigationController pushViewController:third animated:YES];

}

//导航按钮

//右边

//UIBarButtonSystemItemAdd  可选按钮样式

UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(rightItem)];

//赋值给导航栏

self.navigationItem.rightBarButtonItem= item;

//设置当前bar的颜色

item.tintColor= [UIColor redColor];

//左边

UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:selfaction:@selector(leftItem)];

self.navigationItem.leftBarButtonItem= item1;

item1.tintColor= [UIColorredColor];

- (void)rightItem {

NSLog(@"模态");

//模态

NowViewController3 *tvc = [[NowViewController3 alloc] init];

//模态出来一个页面

//[self presentModalViewController:tvc animated:YES];  ios6.0以前使用

//弹出模态(从下往上)

[self presentViewController:tvc animated:YES completion:nil];

}

- (void)dismiss {

//模态对应的模态消失的方法

[self dismissViewControllerAnimated:YES completion:nil];

}

//设置导航栏背景色

//self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:85/225.0 green:105/225.0 blue:77/225.0 alpha:0.2];

//消除导航栏的东西

//[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:0];

//消除导航栏下的分隔线

//self.navigationController.navigationBar.shadowImage= [UIImage new];

你可能感兴趣的:(UINavigationController)