[BS-10] 统一设置app所有页面的“返回”按钮样式

时间:2023-03-08 22:17:51
[BS-10] 统一设置app所有页面的“返回”按钮样式

统一设置app所有页面的“返回”按钮样式

如果想统一设置app所有页面的“返回”按钮样式,首先自定义WZNavigationController类继承UINavigationController类,然后在自定义类中重写pushViewController: animated:方法即可。

//重写navC的pushVC方法,以便统一设置push进来的vc的左侧“返回”按钮
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated { viewController.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil]; //这句会立即调用被push的VC的viewDidLoad方法,必须放在最后面,否则上面的代码会对viewDidLoad中代码造成覆盖,导致viewDidLoad设置无效。
[super pushViewController:viewController animated:animated];//animated换为NO,所有VC没有动画
}

统一设置app所有页面的LeftBarButton按钮样式Demo

//WZNavigationController.h

#import <UIKit/UIKit.h>
@interface WZNavigationController : UINavigationController
@end //WZNavigationController.m #import "WZNavigationController.h" @implementation WZNavigationController //第一次使用该类时调用该方法(只调用一次)
+ (void)initialize { //UINavigationBar *bar = [UINavigationBar appearance];使用这个以后所有其他自定义类的nav都会使用该背景色
UINavigationBar *bar = [UINavigationBar appearanceWhenContainedIn:[self class], nil];//只在本类及子类有效
[bar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];
} - (void)viewDidLoad {
[super viewDidLoad]; //设置导航栏的背景图片(放在这每次创建nav都会调用)
//[self.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault]; //设置导航栏染色描边颜色
//self.navigationBar.tintColor = [UIColor blackColor];
} /**
* 作法1. 苹果默认只能让你在当前vc修改下一vc左侧Back按钮的名字,不能修改字体颜色和高亮颜色,也不能添加自定义按钮;
* 通过self.navigationBar.tintColor = [UIColor blackColor];可以修改“返回”颜色(默认蓝色),但该方法却怎么也实现不了长按“返回”高亮时的红色。
*/ /*
//重写navC的pushVC方法,以便统一设置push进来的vc的左侧“返回”按钮
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated { viewController.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil]; //这句会立即调用被push的VC的viewDidLoad方法,必须放在最后面,否则上面的代码会对viewDidLoad中代码造成覆盖,导致viewDidLoad设置无效。
[super pushViewController:viewController animated:animated];//animated换为NO,所有VC没有动画
}
*/ /**
* 作法2. 因为苹果默认不让添加自定义按钮到本页面管理的下一页面的Back按钮,那么我们可以自定义下一页面的leftBarButtonItem; */ //重写navC的pushVC方法,以便统一设置push进来的vc的左侧“返回”按钮
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated { if(self.viewControllers.count >= )
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.bounds = CGRectMake(, , , ); //必须设置尺寸大小
[btn setTitle:@"返回" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
[btn setImage:[UIImage imageNamed:@"navigationButtonReturn"] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:@"navigationButtonReturnClick"] forState:UIControlStateHighlighted];//此处不需要拉伸,所以setImage,不能错写成BackgroundImage了
//设置btn内容水平靠左
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
//设置-10的内边距,让它更靠左(-10特殊用法)
btn.contentEdgeInsets = UIEdgeInsetsMake(, -, , );
//btn添加事件
[btn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:btn];
//当vc被push进入nav时隐藏底部bar
viewController.hidesBottomBarWhenPushed = YES; } //这句会立即调用被push的VC的viewDidLoad方法,必须放在最后面,否则上面的代码会对viewDidLoad中代码造成覆盖,导致viewDidLoad设置无效。
[super pushViewController:viewController animated:animated];//animated换为NO,所有VC没有动画
} - (void)back {
[self popViewControllerAnimated:YES];
} @end