iOS 7: How to set UIBarButtonItem backButtonBackgroundImage for UIControlStateHighlighted?

前端 未结 3 1718
耶瑟儿~
耶瑟儿~ 2021-02-19 13:26

I am trying to set the background image for back button in normal and highlighted states.

- (void)configureBackButtonInNavigationItem:(UINavigationItem *)item
{
          


        
3条回答
  •  遇见更好的自我
    2021-02-19 13:39

    In addition to l0gg3r's answer, you can make a subclass of UINavigationBar where you can implement l0gg3r's logic and customize your back button.
    After which you just have to set the class name to your navigationBar from storyboard.

    Something like this:

    #import "MyNavigationBar.h"
    
    #import 
    #import     
    
    #pragma mark - UINavigationController category
    
    @interface UINavigationController (InteractiveGesture) 
    
    - (void)fixInteractivePopGesture;
    
    @end
    
    @implementation UINavigationController (InteractiveGesture)
    
    - (void)fixInteractivePopGesture
    {
        __weak UINavigationController *weakSelf = self;
    
        if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
            self.interactivePopGestureRecognizer.delegate = weakSelf;
            self.delegate = weakSelf;
        }
    
        [self swizzleOriginalSelectorWithName:@"pushViewController:animated:" 
                           toSelectorWithName:@"myPushViewController:animated:"];
    }
    
    #pragma mark - Swizzle method
    - (void)swizzleOriginalSelectorWithName:(NSString *)origName toSelectorWithName:(NSString *)swizzleName
    {
        Method origMethod = class_getInstanceMethod([self class], NSSelectorFromString(origName));
        Method newMethod = class_getInstanceMethod([self class], NSSelectorFromString(swizzleName));
        method_exchangeImplementations(origMethod, newMethod);
    }
    
    - (void)myPushViewController:(UIViewController *)viewController animated:(BOOL)animated
    {
        if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
            self.interactivePopGestureRecognizer.enabled = NO;
        }
    
        [self myPushViewController:viewController animated:animated];
    }
    
    #pragma mark UINavigationControllerDelegate
    
    - (void)navigationController:(UINavigationController *)navigationController
           didShowViewController:(UIViewController *)viewController
                        animated:(BOOL)animate
    {
        // Enable the gesture again once the new controller is shown        
        if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
            self.interactivePopGestureRecognizer.enabled = YES;
        }
    }
    
    @end
    
    #pragma mark - MyNavigationBar
    
    @interface MyNavigationBar()
    
    @property (strong, nonatomic) UIButton *backButtonCustomView;
    
    @end
    
    @implementation MyNavigationBar
    
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            [self setup];
        }
        return self;
    }
    
    - (void)awakeFromNib
    {
        [super awakeFromNib];
    
        [self setup];
    }
    
    - (void)setup
    {
        self.backButtonCustomView = [UIButton buttonWithType:UIButtonTypeCustom];
        // here customize your button        
        // e.g. set images for Normal state, or highlighted state, etc...
        // ...
        [self.backButtonCustomView addTarget:self action:@selector(handleBackButton:) forControlEvents:UIControlEventTouchUpInside];
    
        self.backButton = [[UIBarButtonItem alloc] initWithCustomView: self.backButtonCustomView];        
    }
    
    - (void)layoutSubviews
    {
        [super layoutSubviews];
    
        if ([[self navigationController] viewControllers].count > 1) {
            [self.topItem setLeftBarButtonItem:self.backButton animated:YES];
        }
    
        // Enabling back "Swipe from edge to pop" feature.
        [self.navigationController fixInteractivePopGesture];
    }
    
    - (void)handleBackButton:(id)sender
    {
        UINavigationController *nvc = [self navigationController];
    
        [nvc popViewControllerAnimated:YES];
    }
    
    - (UINavigationController *)navigationController
    {
        UINavigationController *resultNC = nil;
        UIViewController *vc = nil;
        for (UIView* next = [self superview]; next; next = next.superview) {
            UIResponder* nextResponder = [next nextResponder];
    
            if ([nextResponder isKindOfClass:[UIViewController class]]) {
                vc = (UIViewController*)nextResponder;
                break;
            }
        }
    
        if (vc) {
            if ([vc isKindOfClass:[UINavigationController class]]) {
                resultNC = (UINavigationController *)vc;
            } else {
                resultNC = vc.navigationController;
            }
        }
    
        return resultNVC;
    }
    
    @end
    

    Then:
    enter image description here

    Here you go. Thats it! Now you can just copy/paste that class into any project you want and just set class name from storyboard :)

提交回复
热议问题