一.导航栏自带一个返回按钮,我们需要定制它的样式,这可以有许多办法。这是苹果文档中的说明:
Note that if you use aUINavigationController object to manage hierarchical navigation, you should notdirectly access the navigation bar object.
这里,自定义导航栏返回按钮步骤如下。
1.首先先把系统的导航栏返回按钮取消.
2.设置导航栏左边按钮.
/**1.隐藏导航栏返回按钮*/
self.navigationItem.hidesBackButton = YES;
/**2.设置导航栏左边Item*/
/**2.1创建一个button*/
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
/**2.2设置frame*/
btn.frame = CGRectMake(15, 5, 38, 20);
/**2.3设置背景图像*/
[btn setBackgroundImage:[UIImage imageNamed:@"btn_nav_back_normal"] forState:UIControlStateNormal];
/**2.4添加单击事件*/
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *back=[[UIBarButtonItem alloc]initWithCustomView:btn];
/**2.5设置导航栏左边item*/
self.navigationItem.leftBarButtonItem = back;
二.设置导航文字大小和文字的颜色
/**1.设置导航标题文字大小和颜色*/
self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor],
NSFontAttributeName : [UIFont systemFontOfSize:14]};
/**2.自定义导航按钮*/
/**2.1创建button*/
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
/**2.2设置图片*/
[button setImage:[UIImage imageNamed:@"Mylottery_config@2x.png"] forState:UIControlStateNormal];
/**2.3设置标题*/
[button setTitle:@"设置" forState:UIControlStateNormal];
/**2.4设置标题与图片间距*/
[button setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 10)];
/**2.5添加事件*/
[button addTarget:self action:@selector(rightBarButtonItemClick) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0,80, 40)];
/**2.6设置导航右边按钮*/
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
}
/**2.7执行事件*/
#pragma mark--导航按钮事件
- (void)rightBarButtonItemClick{
/**跳转到设置页面*/
SettingTableViewController *settingVC = [[SettingTableViewController alloc] init];
[self.navigationController pushViewController:settingVC animated:YES];
}