UINavigationItem的常用方法
(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor whiteColor];//通过文字设置导航条左边按钮
//设置当前导航条左边或者右边中间按钮必须用self.navigationItem
// self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@”文字” style:UIBarButtonItemStylePlain target:self action:@selector(printf:)];//通过系统设置导航条左边按钮
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(printf:)];//通过图片设置导航条右边按钮
// self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@”Fav_Filter_ALL_HL@2x”] style:UIBarButtonItemStylePlain target:self action:@selector(imgNamed:)];//设置中间自定义视图
// self.navigationItem.title = @”hello”;
// self.title = @”world”;
UITextField *textF = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
textF.borderStyle = UITextBorderStyleRoundedRect;
self.navigationItem.titleView = textF;//设置导航条右边按钮自定义
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 50, 30);
[button setImage:[UIImage imageNamed:@”001@2x”] forState:UIControlStateNormal];
button.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];//设置导航条返回按钮
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@”返回” style:UIBarButtonItemStylePlain target:nil action:nil];
}
(void)printf:(UIBarButtonItem *)button
{
NSLog(@”title == %@”,button.title);
// button.title = @”字符”;
SecondViewController *secondVC = [[SecondViewController alloc] init];
[self.navigationController pushViewController:secondVC animated:YES];
}(void)imgNamed:(UIBarButtonItem *)item
{
// item.image
NSLog(@”图片按钮”);
}
//自定义左边按钮 默认隐藏系统按钮
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 50, 30);
[button setImage:[UIImage imageNamed:@"bj_top_jt@2x"] forState:UIControlStateNormal];
button.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 20);
[button addTarget:self action:@selector(pop) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
}
- (void)pop
{
[self.navigationController popViewControllerAnimated:YES];
}
UINavigationItem test
(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor whiteColor];//是否透明
//设置navigationBar为不透明 UI控件的坐标都会自动下移64
self.navigationController.navigationBar.translucent = NO;[self creatUI];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddenView)];
[self.view addGestureRecognizer:tap];
}(void)hiddenView
{
[UIView animateWithDuration:0.5 animations:^{
_view.frame = CGRectMake(0, -126, 375, 126);
} completion:nil];
}(void)creatUI
{
//导航按钮
_button = [UIButton buttonWithType:UIButtonTypeCustom];
_button.frame = CGRectMake(0, 0, 50, 30);
[_button setTitle:@”文字” forState:UIControlStateNormal];
[_button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[_button addTarget:self action:@selector(viewHidden) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.titleView = _button;//背景视图
_view = [[UIView alloc] initWithFrame:CGRectMake(0, -126, 375, 126)];
_view.backgroundColor = [UIColor whiteColor];
[self.view addSubview:_view];//提示文字按钮
NSArray *arr = @[@”文字”,@”图片”,@”音乐”];
for(int i = 0; i < 3; i++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 42 * i, 375, 40);
button.backgroundColor = [UIColor redColor];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitle:arr[i] forState:UIControlStateNormal];
[button addTarget:self action:@selector(changeTitle:) forControlEvents:UIControlEventTouchUpInside];
[_view addSubview:button];
}
}
//提示框动画显示
- (void)viewHidden
{
[UIView animateWithDuration:0.5 animations:^{
_view.frame = CGRectMake(0, 0, 375, 126);
} completion:nil];
}
//修改主标题文字
- (void)changeTitle:(UIButton *)button
{
[_button setTitle:button.currentTitle forState:UIControlStateNormal];
[UIView animateWithDuration:0.5 animations:^{
_view.frame = CGRectMake(0, -126, 375, 126);
} completion:nil];
}