iOS 7: How to set UIBarButtonItem backButtonBackgroundImage for UIControlStateHighlighted?

前端 未结 3 1668
耶瑟儿~
耶瑟儿~ 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:34

    You didn't want a custom view because it would break the swiping, but you should add this line.

    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self; 
    

    Your code would be something like below.

    UIImage *normalImage = [[[UIImage imageNamed:@"btn_normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] resizableImageWithCapInsets:UIEdgeInsetsMake(0.f, 17.f, 0.f, 0.f)];
    UIImage *pressedImage = [[[UIImage imageNamed:@"btn_on_press"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] resizableImageWithCapInsets:UIEdgeInsetsMake(0.f, 17.f, 0.f, 0.f)];    
    
    UIButton *customBackButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [customBackButton setBackgroundImage:normalImage forState:UIControlStateNormal];
    [customBackButton setBackgroundImage:pressedImage forState:UIControlStateHighlighted];
    [customBackButton addTarget:self action:@selector(customBackMethod:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *customBackBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:customBackButton];
    self.navigationItem.leftBarButtonItem = customBackBarButtonItem;
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    
    - (IBAction)customBackMethod:(id)sender {
        [self.navigationController popViewControllerAnimated:YES];
    }
    

提交回复
热议问题