关于ios:iOS自定义返回按钮

33次阅读

共计 3146 个字符,预计需要花费 8 分钟才能阅读完成。

2019/01/09 17:02

在父视图中批改

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

正文完
 0