一、 backBarButtonItem
1 修改系统返回按钮颜色 和文字颜色
在 appdelegate中
UINavigationBar * navigationBar = [UINavigationBar appearance];
//返回按钮的箭头颜色
[navigationBar setTintColor:[UIColor colorWithRed:0.984 green:0.000 blue:0.235 alpha:1.000]];
2 去掉系统返回按钮
在appdelegate中
navigationBar.backIndicatorImage = [[UIImage alloc]init];
navigationBar.backIndicatorTransitionMaskImage = [[UIImage alloc]init];
3. 添加自定义图片
(去掉文字:
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)
forBarMetrics:UIBarMetricsDefault];
)
3.1 在appdelegate中 navigationBar.backIndicatorImage = image;
navigationBar.backIndicatorTransitionMaskImage = image;
方法2
在controler中
UIBarButtonItem * back = [[UIBarButtonItem alloc]init];
UIImage* image = [UIImage imageNamed:@"img_topbar_back"];
[back setBackButtonBackgroundImage:[image resizableImageWithCapInsets:UIEdgeInsetsMake(0, image.size.width, 0, 0)] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
self.navigationItem.backBarButtonItem = back;
4.设置文字
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:nil];
self.navigationItem.backBarButtonItem = back;
设置 文字偏移
UIOffset offset;
offset.horizontal = 200;
offset.vertical = 0;
//
[backButton setBackButtonTitlePositionAdjustment:offset forBarMetrics:UIBarMetricsDefault];
设置 文字颜色
在当前controler中修改
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
二、initWithCustomView (可调按钮位置,原理同第三)
UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [leftBtn setTitle:@"返回" forState:UIControlStateNormal]; [leftBtn addTarget:self action:@selector(nongshali) forControlEvents:UIControlEventTouchUpInside];
leftBtn.frame = CGRectMake(0, 0, 80, 40);
// [leftBtn setImage:[UIImage imageNamed:@"img_topbar_back2"] forState:UIControlStateNormal];
leftBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
// leftBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
[leftBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; //将leftItem设置为自定义按钮
UIBarButtonItem *leftItem =[[UIBarButtonItem alloc ]initWithCustomView: leftBtn];
self.navigationItem.leftBarButtonItem = leftItem;
此时 ,没有侧滑手势
想要打开侧滑手势
方法1
self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
但此时只能在屏幕边缘滑
方法2 可以整瓶滑
id target = self.navigationController.interactivePopGestureRecognizer.delegate;
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];
pan.delegate = self;
[self.view addGestureRecognizer:pan];
self.navigationController.interactivePopGestureRecognizer.enabled = NO;//屏蔽系统侧滑手势
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
return YES;//返回NO 则屏蔽侧滑手势
}
推荐阅读
http://www.jianshu.com/p/78fc64d387ec
http://www.360doc.com/content/15/0608/18/11417867_476604808.shtml
三、右侧按钮 (可调按钮位置,调整左侧按钮位置类似)
UIButton * rightBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 44, 28)];
[rightBtn setTitle:@"提交" forState:UIControlStateNormal];
[rightBtn setTitleColor:[QBTools colorFromHexCode:@"ffffff"] forState:UIControlStateNormal];
[rightBtn setBackgroundColor:[QBTools colorFromHexCode:@"ff600a"]];
rightBtn.layer.cornerRadius = 2;
rightBtn.layer.masksToBounds = YES;
UIBarButtonItem * rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil action:nil];
negativeSpacer.width = -5;
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:negativeSpacer,rightItem, nil];