导航栏的隐藏与显示
self.navigationController.navigationBarHidden = NO; //有效
[self.navigationController setNavigationBarHidden:NO animated:YES];//有效
self.navigationController.navigationBar.hidden = NO; //无效
@implementation HomeViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view.
UIButton *button = [[UIButtonalloc]initWithFrame:CGRectMake(100,100, 200, 40)];
button.layer.cornerRadius =8;
button.backgroundColor = [UIColorgreenColor];
[button setTitle:@"push"forState:UIControlStateNormal];
[button addTarget:selfaction:@selector(pushVC)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:button];
//定义导航栏上左侧“按钮”类型
UIBarButtonItem *leftItem = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarkstarget:selfaction:@selector(study)];
/*一个导航控制器控制若干个视图控制器
*一个导航控制器控制包含一个NavigationBar和一个toolBar
*NavigationBar中“按钮”是一个NavigationItem
*通过设置NavigationItem的属性,显示Item(在NavigationBar上)
*NavigationItem不是由NavigationBar控制,也是由NavigationController控制,而是由视图控制器控制
*/
//错误的写法
// self.navigationController.navigationItem.leftBarButtonItem = leftItem;
self.navigationItem.leftBarButtonItem = leftItem;
//设置导航栏上右侧“控件”属性
UIButton *item = [[UIButtonalloc]initWithFrame:CGRectMake(100,100, 60, 30)]; //x,y设置无用,可设置宽,高
item.layer.cornerRadius =8;
item.backgroundColor = [UIColorgreenColor];
[item setTitle:@"test"forState:UIControlStateNormal];
[item addTarget:selfaction:@selector(Test)forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightItem = [[UIBarButtonItemalloc]initWithCustomView:item];
self.navigationItem.rightBarButtonItem = rightItem;
UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(0,0, 100, 40)];
label.text = @"首页";
label.textAlignment =NSTextAlignmentCenter;
self.navigationItem.titleView = label;
}
-(void)study{
UIAlertView *alertView = [[UIAlertViewalloc]initWithTitle:@"提示"message:@"要继续学习吗"delegate:selfcancelButtonTitle:@"不要"otherButtonTitles:@"要",nil];
[alertView show];
}
-(void)Test{
UIActionSheet *ActionSh = [[UIActionSheetalloc]initWithTitle:@"写字吗"delegate:selfcancelButtonTitle:@"不写"destructiveButtonTitle:@"重写"otherButtonTitles:@"写第一节",@"写第二节",nil];
[ActionSh showInView:self.view];
}
-(void)pushVC{
UIViewController *secondVC = [[SecondViewControlleralloc]init];
secondVC.view.backgroundColor = [UIColorcyanColor];
//跳转至子页面
[self.navigationControllerpushViewController:secondVC animated:YES];
}
@implementation SecondViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view.
UIButton *button = [[UIButtonalloc]initWithFrame:CGRectMake(100,100, 200, 40)];
button.layer.cornerRadius =8;
button.backgroundColor = [UIColorpurpleColor];
[button setTitle:@"second"forState:UIControlStateNormal];
[button addTarget:selfaction:@selector(backVC)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:button];
UIButton *button2 = [[UIButtonalloc]initWithFrame:CGRectMake(100,200, 200, 40)];
button2.layer.cornerRadius =8;
button2.backgroundColor = [UIColororangeColor];
[button2 setTitle:@"push"forState:UIControlStateNormal];
[button2 addTarget:selfaction:@selector(pushVC3)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:button2];
UIBarButtonItem *rightItem = [[UIBarButtonItemalloc]initWithTitle:@"自定义"style:UIBarButtonItemStyleBorderedtarget:selfaction:nil];
self.navigationItem.rightBarButtonItem = rightItem;
UIBarButtonItem *leftItem = [[UIBarButtonItemalloc]initWithTitle:@"root"style:UIBarButtonItemStyleDonetarget:selfaction:@selector(backVC1)];
self.navigationItem.leftBarButtonItem = leftItem;
UIImageView *titleView = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0, 100,44)];
titleView.image = [UIImageimageNamed:@"button_ok"];
self.navigationItem.titleView = titleView;
}
-(void)backVC1{
[self.navigationControllerpopViewControllerAnimated:YES];
}
-(void)backVC{
//导航栏隐藏
if (self.navigationController.toolbarHidden) {
[self.navigationControllersetToolbarHidden:NOanimated:YES];
[self.navigationControllersetNavigationBarHidden:NOanimated:YES];
}else
{
[self.navigationControllersetToolbarHidden:YESanimated:YES];
[self.navigationControllersetNavigationBarHidden:YESanimated:YES];
}
}
-(void)pushVC3{
UIViewController *threeVC = [[ThreeViewControlleralloc]init];
threeVC.view.backgroundColor = [UIColoryellowColor];
//跳转至子页面
[self.navigationControllerpushViewController:threeVC animated:YES];
}
接跳传页面的threeView