標籤:selector nta hidden 重寫 dap gif action color 圖片
在用navigationVC時,返回按鈕有時候不想用系統的,這裡用繼承的方式把按鈕替換了,同時也可以實現系統的右滑返回,很簡單;
1.建立基類 BasePopViewController
建立一個用於替換系統返回按鈕基類的 BasePopViewController : UIViewController;
BasePopViewController.m
#import "BasePopViewController.h"@interface BasePopViewController ()@end@implementation BasePopViewController- (void)viewDidLoad { [super viewDidLoad]; [self setNavigationItemBackButton]; self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;}- (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; if (self.navigationController.viewControllers.count > 1) { self.navigationController.interactivePopGestureRecognizer.enabled = YES; }else{ self.navigationController.interactivePopGestureRecognizer.enabled = NO; }}/** 自訂狀態列 */- (void)setNavigationItemBackButton{ if (self.navigationController.viewControllers.count > 1) { UIBarButtonItem *back = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"back_off"] style:UIBarButtonItemStylePlain target:self action:@selector(popBackButtonAction)]; self.navigationItem.leftBarButtonItems = @[back]; }}/** 返回按鈕事件 */- (void)popBackButtonAction { [self.navigationController popViewControllerAnimated:YES];}#pragma mark - 下面是設定的狀態列顏色,可忽略-(UIStatusBarStyle)preferredStatusBarStyle{ ///這裡設定白色 return UIStatusBarStyleLightContent;}-(BOOL)prefersStatusBarHidden{ return NO;}@end
2.引用
在需要替換系統的返回按鈕時,建立VC繼承BasePopViewController即可,如果要在新的VC中擷取點擊的返回按鈕事件,在新的VC中重寫 popBackButtonAction 事件即可。
iOS-自訂NavigationItem返回按鈕【pop返回按鈕】