IOS学习 NSNavigationController 自定义:左右按钮、中间文字、背景图片
2016-03-17 16:45
811 查看
@implementation HomeViewController
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view.
UIButton *button = [[UIButton
alloc]initWithFrame:CGRectMake(100,
100, 200,
40)];
button.layer.cornerRadius =
8;
button.backgroundColor = [UIColor
greenColor];
[button setTitle:@"push"
forState:UIControlStateNormal];
[button addTarget:self
action:@selector(pushVC)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:button];
//定义导航栏上左侧“按钮”类型
UIBarButtonItem *leftItem = [[UIBarButtonItem
alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks
target:self
action:@selector(study)];
/*一个导航控制器控制若干个视图控制器
*一个导航控制器控制包含一个NavigationBar和一个toolBar
*NavigationBar中“按钮”是一个NavigationItem
*通过设置NavigationItem的属性,显示Item(在NavigationBar上)
*NavigationItem不是由NavigationBar控制,也是由NavigationController控制,而是由视图控制器控制
*/
//错误的写法
// self.navigationController.navigationItem.leftBarButtonItem = leftItem;
self.navigationItem.leftBarButtonItem = leftItem;
//设置导航栏上右侧“控件”属性
UIButton *item = [[UIButton
alloc]initWithFrame:CGRectMake(100,
100, 60,
30)]; //x,y设置无用,可设置宽,高
item.layer.cornerRadius =
8;
item.backgroundColor = [UIColor
greenColor];
[item setTitle:@"test"
forState:UIControlStateNormal];
[item addTarget:self
action:@selector(Test)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightItem = [[UIBarButtonItem
alloc]initWithCustomView:item];
self.navigationItem.rightBarButtonItem = rightItem;
UILabel *label = [[UILabel
alloc]initWithFrame:CGRectMake(0,
0, 100,
40)];
label.text = @"首页";
label.textAlignment =
NSTextAlignmentCenter;
self.navigationItem.titleView = label;
}
-(void)study{
UIAlertView *alertView = [[UIAlertView
alloc]initWithTitle:@"提示"
message:@"要继续学习吗"
delegate:self
cancelButtonTitle:@"不要"
otherButtonTitles:@"要",
nil];
[alertView show];
}
-(void)Test{
UIActionSheet *ActionSh = [[UIActionSheet
alloc]initWithTitle:@"写字吗"
delegate:self
cancelButtonTitle:@"不写"
destructiveButtonTitle:@"重写"
otherButtonTitles:@"写第一节",@"写第二节",
nil];
[ActionSh showInView:self.view];
}
-(void)pushVC{
UIViewController *secondVC = [[SecondViewController
alloc]init];
secondVC.view.backgroundColor = [UIColor
cyanColor];
//跳转至子页面
[self.navigationController
pushViewController:secondVC animated:YES];
}
@implementation SecondViewController
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view.
UIButton *button = [[UIButton
alloc]initWithFrame:CGRectMake(100,
100, 200,
40)];
button.layer.cornerRadius =
8;
button.backgroundColor = [UIColor
purpleColor];
[button setTitle:@"second"
forState:UIControlStateNormal];
[button addTarget:self
action:@selector(backVC)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:button];
UIButton *button2 = [[UIButton
alloc]initWithFrame:CGRectMake(100,
200, 200,
40)];
button2.layer.cornerRadius =
8;
button2.backgroundColor = [UIColor
orangeColor];
[button2 setTitle:@"push"
forState:UIControlStateNormal];
[button2 addTarget:self
action:@selector(pushVC3)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:button2];
UIBarButtonItem *rightItem = [[UIBarButtonItem
alloc]initWithTitle:@"自定义"
style:UIBarButtonItemStyleBordered
target:self
action:nil];
self.navigationItem.rightBarButtonItem = rightItem;
UIBarButtonItem *leftItem = [[UIBarButtonItem
alloc]initWithTitle:@"root"
style:UIBarButtonItemStyleDone
target:self
action:@selector(backVC1)];
self.navigationItem.leftBarButtonItem = leftItem;
UIImageView *titleView = [[UIImageView
alloc]initWithFrame:CGRectMake(0,
0, 100,44)];
titleView.image = [UIImage
imageNamed:@"button_ok"];
self.navigationItem.titleView = titleView;
}
-(void)backVC1{
[self.navigationController
popViewControllerAnimated:YES];
}
-(void)backVC{
//导航栏隐藏
if (self.navigationController.toolbarHidden)
{
[self.navigationController
setToolbarHidden:NO
animated:YES];
[self.navigationController
setNavigationBarHidden:NO
animated:YES];
}else
{
[self.navigationController
setToolbarHidden:YES
animated:YES];
[self.navigationController
setNavigationBarHidden:YES
animated:YES];
}
}
-(void)pushVC3{
UIViewController *threeVC = [[ThreeViewController
alloc]init];
threeVC.view.backgroundColor = [UIColor
yellowColor];
//跳转至子页面
[self.navigationController
pushViewController:threeVC animated:YES];
}
接跳传页面的threeView
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view.
UIButton *button = [[UIButton
alloc]initWithFrame:CGRectMake(100,
100, 200,
40)];
button.layer.cornerRadius =
8;
button.backgroundColor = [UIColor
greenColor];
[button setTitle:@"push"
forState:UIControlStateNormal];
[button addTarget:self
action:@selector(pushVC)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:button];
//定义导航栏上左侧“按钮”类型
UIBarButtonItem *leftItem = [[UIBarButtonItem
alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks
target:self
action:@selector(study)];
/*一个导航控制器控制若干个视图控制器
*一个导航控制器控制包含一个NavigationBar和一个toolBar
*NavigationBar中“按钮”是一个NavigationItem
*通过设置NavigationItem的属性,显示Item(在NavigationBar上)
*NavigationItem不是由NavigationBar控制,也是由NavigationController控制,而是由视图控制器控制
*/
//错误的写法
// self.navigationController.navigationItem.leftBarButtonItem = leftItem;
self.navigationItem.leftBarButtonItem = leftItem;
//设置导航栏上右侧“控件”属性
UIButton *item = [[UIButton
alloc]initWithFrame:CGRectMake(100,
100, 60,
30)]; //x,y设置无用,可设置宽,高
item.layer.cornerRadius =
8;
item.backgroundColor = [UIColor
greenColor];
[item setTitle:@"test"
forState:UIControlStateNormal];
[item addTarget:self
action:@selector(Test)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightItem = [[UIBarButtonItem
alloc]initWithCustomView:item];
self.navigationItem.rightBarButtonItem = rightItem;
UILabel *label = [[UILabel
alloc]initWithFrame:CGRectMake(0,
0, 100,
40)];
label.text = @"首页";
label.textAlignment =
NSTextAlignmentCenter;
self.navigationItem.titleView = label;
}
-(void)study{
UIAlertView *alertView = [[UIAlertView
alloc]initWithTitle:@"提示"
message:@"要继续学习吗"
delegate:self
cancelButtonTitle:@"不要"
otherButtonTitles:@"要",
nil];
[alertView show];
}
-(void)Test{
UIActionSheet *ActionSh = [[UIActionSheet
alloc]initWithTitle:@"写字吗"
delegate:self
cancelButtonTitle:@"不写"
destructiveButtonTitle:@"重写"
otherButtonTitles:@"写第一节",@"写第二节",
nil];
[ActionSh showInView:self.view];
}
-(void)pushVC{
UIViewController *secondVC = [[SecondViewController
alloc]init];
secondVC.view.backgroundColor = [UIColor
cyanColor];
//跳转至子页面
[self.navigationController
pushViewController:secondVC animated:YES];
}
@implementation SecondViewController
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view.
UIButton *button = [[UIButton
alloc]initWithFrame:CGRectMake(100,
100, 200,
40)];
button.layer.cornerRadius =
8;
button.backgroundColor = [UIColor
purpleColor];
[button setTitle:@"second"
forState:UIControlStateNormal];
[button addTarget:self
action:@selector(backVC)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:button];
UIButton *button2 = [[UIButton
alloc]initWithFrame:CGRectMake(100,
200, 200,
40)];
button2.layer.cornerRadius =
8;
button2.backgroundColor = [UIColor
orangeColor];
[button2 setTitle:@"push"
forState:UIControlStateNormal];
[button2 addTarget:self
action:@selector(pushVC3)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:button2];
UIBarButtonItem *rightItem = [[UIBarButtonItem
alloc]initWithTitle:@"自定义"
style:UIBarButtonItemStyleBordered
target:self
action:nil];
self.navigationItem.rightBarButtonItem = rightItem;
UIBarButtonItem *leftItem = [[UIBarButtonItem
alloc]initWithTitle:@"root"
style:UIBarButtonItemStyleDone
target:self
action:@selector(backVC1)];
self.navigationItem.leftBarButtonItem = leftItem;
UIImageView *titleView = [[UIImageView
alloc]initWithFrame:CGRectMake(0,
0, 100,44)];
titleView.image = [UIImage
imageNamed:@"button_ok"];
self.navigationItem.titleView = titleView;
}
-(void)backVC1{
[self.navigationController
popViewControllerAnimated:YES];
}
-(void)backVC{
//导航栏隐藏
if (self.navigationController.toolbarHidden)
{
[self.navigationController
setToolbarHidden:NO
animated:YES];
[self.navigationController
setNavigationBarHidden:NO
animated:YES];
}else
{
[self.navigationController
setToolbarHidden:YES
animated:YES];
[self.navigationController
setNavigationBarHidden:YES
animated:YES];
}
}
-(void)pushVC3{
UIViewController *threeVC = [[ThreeViewController
alloc]init];
threeVC.view.backgroundColor = [UIColor
yellowColor];
//跳转至子页面
[self.navigationController
pushViewController:threeVC animated:YES];
}
接跳传页面的threeView
相关文章推荐
- 3.2-nagios自定义监控系统磁盘脚本
- iOS 定义多个参数函数的写法
- IOS 返回JSON数据中存在NULL的解决办法
- iOS 打电话
- iOS 开发小记(十二)
- iOS粒子效果
- iOS中的日期时间
- 关于iOS多线程
- IOS-57-导致内存未释放的常见原因(现象:dealloc不执行等)
- 字典转成字符串iOS
- iOS 开发常用的开源组件
- 使用导航控制器 返回上级界面iOS
- iOS ScrollView 计算页数跳转
- iOS 四舍五入
- iOS开发-OpenGLES 入门的大杂烩
- 89.ios开发之block的使用,及注意事项
- iOS数据存储之对象归档
- IOS加急审核及理由
- iOS开发前要知道的事儿
- iOS通知栏小工具开发