iOS11 导航栏自定义按钮

一、全局自定义导航栏左侧按钮
UIImage *backButtonImage = [[UIImage imageNamed:@"nav_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
   [UINavigationBar appearance].backIndicatorImage = backButtonImage;
   [UINavigationBar appearance].backIndicatorTransitionMaskImage = backButtonImage;
效果图啦~.png
二、在某个页面需要另外自定义导航栏左侧按钮

在iOS11以前可以用:

UIBarButtonItem *negativeSeperator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
   negativeSeperator.width = 8;
   self.navigationItem.leftBarButtonItems = @[[[UIBarButtonItem alloc] 
initWithCustomView:button],negativeSeperator];  

iOS11之后呢,这个失效啦,可以用一下方法解决(也许还有其他办法的):

UIImage *backButtonImage = [[UIImage imageNamed:@"nav_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
   self.navigationController.navigationBar.backIndicatorImage = backButtonImage;
   self.navigationController.navigationBar.backIndicatorTransitionMaskImage = backButtonImage;
三、给导航栏的返回按钮添加自定义事件

1.给UIViewController写一个类别:UIViewController+BackButtonHandler
.h文件

@protocol BackButtonHandlerProtocol 
@optional
-(BOOL)navigationShouldPopOnBackButton;
@end
@interface UIViewController (BackButtonHandler) 
@end

.m文件

#import "UIViewController+BackButtonHandler.h"

@implementation UIViewController (BackButtonHandler)

@end

@implementation UINavigationController (ShouldPopOnBackButton)

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item
{
   if([self.viewControllers count] < [navigationBar.items count]) {
       return YES;
   }
   
   BOOL shouldPop = YES;
   UIViewController* vc = [self topViewController];
   if([vc respondsToSelector:@selector(navigationShouldPopOnBackButton)]) {
       shouldPop = [vc navigationShouldPopOnBackButton];
   }
   
   if(shouldPop) {
       dispatch_async(dispatch_get_main_queue(), ^{
           [self popViewControllerAnimated:YES];
       });
   } else {
       for(UIView *subview in [navigationBar subviews]) {
           if(0. < subview.alpha && subview.alpha < 1.) {
               [UIView animateWithDuration:.25 animations:^{
                   subview.alpha = 1.;
               }];
           }
       }
   }
   return NO;
}
@end

在controller里面使用:

- (BOOL)navigationShouldPopOnBackButton
{
    [self.navigationController popToRootViewControllerAnimated:YES];
     return YES;
} 
四、修改手势
@property (nonatomic, assign) id originalDelegate;
@property (nonatomic, assign) BOOL originalEnabled;

- (void)viewDidLoad {
   [super viewDidLoad];

   UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(navigationShouldPopOnBackButton)];
   [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
   [self.view addGestureRecognizer:recognizer];
}

- (void)viewWillAppear:(BOOL)animated {
   [super viewWillAppear:animated];
  
   //禁止右侧返回
   self.originalDelegate = self.navigationController.interactivePopGestureRecognizer.delegate;
   self.originalEnabled = self.navigationController.interactivePopGestureRecognizer.enabled;
   
   self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
   self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}

- (void)viewWillDisappear:(BOOL)animated {
   [super viewWillDisappear:animated];
   
   //开启右滑返回
   if (self.originalDelegate) {
       self.navigationController.interactivePopGestureRecognizer.delegate = self.originalDelegate;
       self.navigationController.interactivePopGestureRecognizer.enabled = self.originalEnabled;
   }
} 

你可能感兴趣的:(iOS11 导航栏自定义按钮)