博客搬家,2018
在父视图中修改
1、使用 backBarButtonItem 实现,返回按钮紧贴屏幕左边,有返回手势。
这种方法需要将图片设置成 UIImageRenderingModeAlwaysOriginal 模式才能正确显示,否则图片会被渲染成蓝色。
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationController.navigationBar.backIndicatorImage = [[UIImage imageNamed:@"btn_back_black"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.navigationController.navigationBar.backIndicatorTransitionMaskImage = [[UIImage imageNamed:@"btn_back_black"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.navigationItem.backBarButtonItem = backItem;
}
2、使用 leftBarButtonItem 实现,返回按钮与屏幕左边缘有一小段距离(大概为8),有返回手势。
#import "BaseViewController.h"
@interface BaseViewController ()
@property (nonatomic, strong) UIBarButtonItem *backButtonItem;
@end
@implementation BaseViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.hidesBackButton = YES;
if (self.navigationController.viewControllers.count > 1) {
[self backButtonItemImageNamed:@"btn_back_black" target:self action:@selector(backButtonItemClicked:)];
}
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
//解决根试图左边滑动手势页面卡死问题
if (self.navigationController.viewControllers.count == 1) {
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
} else {
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
}
}
- (void)backButtonItemImageNamed:(NSString*)imgName target:(id)target action:(SEL)selector {
UIImage* backImg = [[UIImage imageNamed:imgName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithImage:backImg style:UIBarButtonItemStylePlain target:target action:selector];
self.backButtonItem = item;
self.navigationItem.leftBarButtonItem = self.backButtonItem;
//启用返回手势,需要解决卡死问题
self.navigationController.interactivePopGestureRecognizer.delegate = nil;
}
- (void)backButtonItemClicked:(UIButton *)sender {
[self.navigationController popViewControllerAnimated:YES];
}
@end
在本视图中修改
1、修改成只有图片的:
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"箭头left40x40"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
self.navigationItem.leftBarButtonItem = backItem;
2、修改成只有文字的
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(back)];
self.navigationItem.leftBarButtonItem = backItem;
3、自定义文字和图片的:
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(0, 0, 30, 30);
[backButton setTitle:@"返回" forState:UIControlStateNormal];
[backButton setImage:[[UIImage imageNamed:@"箭头left40x40"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal]
[backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
注意:上面这3种方法都会使返回按钮偏右,不大美观;而且会使系统自带的右滑手势返回移除控制的功能失效,解决办法就是在后面添加一句代码,让导航控制器重新设置这个功能,解决代码如下:
self.navigationController.interactivePopGestureRecognizer.delegate = nil;
以上这些方法都是只是在一个视图中生效
全局设置返回按钮
大多数时候自定义返回按钮都是全局一样的,所以全局设置的方法请参考:
iOS 自定义 NavigationController
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。