您的位置:首页 > 移动开发 > IOS开发

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: